美文网首页
数据存储到SQLite数据库中

数据存储到SQLite数据库中

作者: 在下陈小村 | 来源:发表于2018-02-27 10:44 被阅读9次

    数据存储到SQLite中需要重写一个SQLiteOpenHelper类,包含创建和升级两个方法,具体代码如下:

    public class MyDatabaseHelper extends SQLiteOpenHelper {
        private Context mContext;
        private String CREATE_BOOK="create table book(id integer primary key autoincrement,author text,price real,pages integer,name text)";
        private String CREATE_CATEGORY="create table category(id integer primary key autoincrement,category_name text,category_code integer)";
        public MyDatabaseHelper(Context context, String name, SQLiteDatabase.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,"数据库创建成功",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);
        }
    }
    

    当然还有增删改查方法,getWritableDatabase()的作用是如果数据库不存在则创建一个,如果存在则打开已创建的数据库

    public class SQLiteActivity extends AppCompatActivity implements View.OnClickListener{
    
        private MyDatabaseHelper myDatabaseHelper;
        private SQLiteDatabase db;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sqlite);
            myDatabaseHelper = new MyDatabaseHelper(SQLiteActivity.this,"Book.db",null,4);
            findViewById(R.id.createDB).setOnClickListener(this);
            findViewById(R.id.insert).setOnClickListener(this);
            findViewById(R.id.update).setOnClickListener(this);
            findViewById(R.id.delete).setOnClickListener(this);
            findViewById(R.id.query).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.createDB:
                    myDatabaseHelper.getWritableDatabase();
                    break;
                case R.id.insert:
                    db= myDatabaseHelper.getWritableDatabase();
                    db.execSQL("insert into book (name,author,pages,price) values (?,?,?,?)",new String[]{"第一行代码","郭林","600","50"});
                    db.execSQL("insert into book (name,author,pages,price) values (?,?,?,?)",new String[]{"第二行代码","郭林","700","60"});
    //                ContentValues values=new ContentValues();
    //                values.put("name","第一行代码");
    //                values.put("author","郭林");
    //                values.put("pages","600");
    //                values.put("price","50");
    //                db.insert("book",null,values);
                    Toast.makeText(SQLiteActivity.this,"添加数据成功",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.update:
                    db= myDatabaseHelper.getWritableDatabase();
                    db.execSQL("update book set price=? where name=?",new String[]{"30","第一行代码"});
                    Toast.makeText(SQLiteActivity.this,"修改数据成功",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.delete:
                    db= myDatabaseHelper.getWritableDatabase();
                    db.execSQL("delete from book where pages>?",new String[]{"600"});
                    Toast.makeText(SQLiteActivity.this,"删除数据成功",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.query:
                    db= myDatabaseHelper.getWritableDatabase();
                    Cursor cursor=db.rawQuery("select * from 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"));
                            Toast.makeText(SQLiteActivity.this,name+author+pages+price,Toast.LENGTH_SHORT).show();
                        }while (cursor.moveToNext());
                    }
                    Toast.makeText(SQLiteActivity.this,"查询数据成功",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:数据存储到SQLite数据库中

          本文链接:https://www.haomeiwen.com/subject/yzvyxftx.html