持久化存储--3、SQLite 数据库存储

持久化存储--3、SQLite 数据库存储,第1张

概述SQLite:轻量级的关系型数据库,android提供SQLiteOpenHelper帮助类,方便对数据库创建和升级。抽象方法onCreate()和 onUpgrade(),创建和升级 数据库逻辑。 实例方法getReadableDatabase() 和 getWritableDatabase(),创建或打开现有数据库。 public class MyDatabaseHelper extends

sqlite:轻量级的关系型数据库,androID提供sqliteOpenHelper帮助类,方便对数据库创建和升级。抽象方法onCreate()和 onUpgrade(),创建和升级
数据库逻辑。
实例方法getReadableDatabase() 和
getWritableDatabase(),创建或打开现有数据库。

public class MyDatabaseHelper extends sqliteOpenHelper {    public static final String CREATE_BOOK = "create table Book ("            + "ID integer primary key autoincrement,"             + "author text,"            + "price real,"             + "pages integer,"             + "name text)";    public static final String CREATE_category = "create table category ("            + "ID integer primary key autoincrement,"            + "category_name text,"            + "category_code integer)";    private Context mContext;    //重写构造方法:Context,数据库名,查询数据时返回自定义Cursor,数据库版本号    public MyDatabaseHelper(Context context,String name,CursorFactory factory,int version) {        super(context,name,factory,version);        mContext = context;    }    @OverrIDe    public voID onCreate(sqliteDatabase db) {        db.execsql(CREATE_BOOK);        db.execsql(CREATE_category);        Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();    }    @OverrIDe    public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {        db.execsql("drop table if exists Book");        db.execsql("drop table if exists category");        onCreate(db);    }}

1、创建数据库:

dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);        button createDatabase = (button) findVIEwByID(R.ID.create_database);        createDatabase.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                dbHelper.getWritableDatabase();            }        });

生成BookStore.db数据库

使用sqliteSpy软件打开,可以看到Book表,里面有5种数据类型

2、添加数据

button addData = (button) findVIEwByID(R.ID.add_data);        addData.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                sqliteDatabase db = dbHelper.getWritableDatabase();                ContentValues values = new ContentValues();                values.put("name","The Da Vinci Code");                values.put("author","Dan brown");                values.put("pages",454);                values.put("price",16.96);                db.insert("Book",null,values);                values.clear();                values.put("name","The Lost Symbol");                values.put("author",510);                values.put("price",19.95);                db.insert("Book",values);            }        });

可以看到数据库里面的数据

3、更新数据

button updateData = (button) findVIEwByID(R.ID.update_data);        updateData.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                sqliteDatabase db = dbHelper.getWritableDatabase();                ContentValues values = new ContentValues();                values.put("price",10.99);                db.update("Book",values,"name = ?",new String[] { "The Da Vinci Code" });            }        });

更新数据,可以看到价格变成了10.99

4、删除数据

button deletebutton = (button) findVIEwByID(R.ID.delete_data);        deletebutton.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                sqliteDatabase db = dbHelper.getWritableDatabase();                db.delete("Book","pages > ?",new String[] { "500" });            }        });

删除数据,可以看到name数据The Lost Symbol被删除,其他项也被删除:

5、遍历数据

button querybutton = (button) findVIEwByID(R.ID.query_data);        querybutton.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                sqliteDatabase db = dbHelper.getWritableDatabase();                Cursor cursor = db.query("Book",null);                if (cursor.movetoFirst()) {                    do {                        String name = cursor.getString(cursor                                .getColumnIndex("name"));                        String author = cursor.getString(cursor                                .getColumnIndex("author"));                        int pages = cursor.getInt(cursor                                .getColumnIndex("pages"));                        double price = cursor.getDouble(cursor                                .getColumnIndex("price"));                        Log.d("MainActivity","book name is " + name);                        Log.d("MainActivity","book author is " + author);                        Log.d("MainActivity","book pages is " + pages);                        Log.d("MainActivity","book price is " + price);                    } while (cursor.movetoNext());                }                cursor.close();            }        });

设置断点,deBUG模式下,单击query按键,运行到断点处,可以在watch窗口看到各个变量的数据:

6、代替数据

button replaceData = (button) findVIEwByID(R.ID.replace_data);        replaceData.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                sqliteDatabase db = dbHelper.getWritableDatabase();                db.beginTransaction();                try {                    db.delete("Book",null);// if (true) {// throw new NullPointerException();// }                    ContentValues values = new ContentValues();                    values.put("name","Game of Thrones");                    values.put("author","George Martin");                    values.put("pages",720);                    values.put("price",20.85);                    db.insert("Book",values);                    db.setTransactionSuccessful();                } catch (Exception e) {                    e.printstacktrace();                } finally {                    db.endTransaction();                }            }        });

可以看到name数据被代替:

总结

以上是内存溢出为你收集整理的持久化存储--3、SQLite 数据库存储全部内容,希望文章能够帮你解决持久化存储--3、SQLite 数据库存储所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1170378.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存