• SQLiteDatabase 和 SQLiteOpenHelper


    SQLiteDatabase类可以执行SQL语句,对数据库进行增删改查。

    SQLiteOpenHelper是一个辅助类,帮助SQLiteDatabase获取对数据库的操作权。它有两个重写的方法,一个是onCreate()创建数据库,另一个是OnUpgrage() 用户升级数据库使用

    实例:

    1.写一个SQLiteOpenHelper创建数据库:

    创建数据的sql语句模式是:
    名称的数据格式需要注意,特别是text类需要在使用的时候添加单引号,否则报错;
    未免这种错失,可以使用占位符方式,看例子2

    create table  XX (XX integer primary key autoincrement,XX text not null,XX integer not null)
    package com.man.SQLiteDemoOne;

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;



    public class DBhelper extends SQLiteOpenHelper {

    public DBhelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table if not exists demo(_id integer primary key autoincrement,name text not null, age integer not null)");
    db.execSQL("insert into demo(name,age)values('Tom',25)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("drop table if exists demo");
    onCreate(db);
    }
    }

    2.写一个增删改查的类以及其中的方法:

    注意,text的引号使用

    package com.man.SQLiteDemoOne;


    import android.database.sqlite.SQLiteDatabase;

    public class MyOperation {

    private SQLiteDatabase db = null;

    public MyOperation(SQLiteDatabase db) {

    this.db = db;
    }

    public void insertData(String name, int age) {

    db.execSQL("insert into demo (name,age) values('"+name+"',"+age+")");
    }

    public void deleteData(int id){

    db.execSQL("delete from demo where _id="+id);
    }

    public void updateData(int id,String name ,int age){

    db.execSQL("update demo set name='" + name+"',age=" + age + " where _id=" + id);
    }

    }
    3.在main类中调用:

    package com.man.SQLiteDemoOne;

    import android.app.Activity;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;

    public class MyActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DBhelper helper = new DBhelper(MyActivity.this, "dog.db", null, 1);
    SQLiteDatabase db = helper.getWritableDatabase();
    MyOperation operation = new MyOperation(db);

    operation.updateData(10, "change", 55);
    operation.deleteData(15);
    operation.deleteData(14);
    db.close();

    }
    }


    例子二:为了避免使用string的标点符号导致sql语句错误,用占位符

    package com.example.SQlistDemoTwo;

    import android.database.sqlite.SQLiteDatabase;


    public class Methodss {

    private SQLiteDatabase db;

    public Methodss(SQLiteDatabase db) {
    this.db = db;
    }

    public void insertData(String name,int age) {

    Object[] item = new Object[]{name,age};
    db.execSQL("insert into stu(name,age) values(?,?)",item);
    }

    public void updateData(int id,String name,int age){

    Object[] items = new Object[]{name, age, id};
    db.execSQL("update stu set name=?,age=? where id=?",items);
    }

    public void deleteData(int id) {

    Object[] items = new Object[]{id};
    db.execSQL("delete from stu where id=?",items);
    }
    }




  • 相关阅读:
    内存屏障指令
    k64 datasheet学习笔记45---10/100-Mbps Ethernet MAC(ENET)之功能描述
    k64 datasheet学习笔记45---10/100-Mbps Ethernet MAC(ENET)之概述
    Haskell 笔记 ③
    UVA 11754 (暴力+中国剩余定理)
    Haskell 笔记 ②
    Haskell 笔记 ①
    HDU 1072(记忆化BFS)
    POJ 2096 (概率DP)
    HDU 4405 (概率DP)
  • 原文地址:https://www.cnblogs.com/share2015/p/5271625.html
Copyright © 2020-2023  润新知