1:创建一个类,继承SQLiteOpenHelper这个抽象类;代码如下
下面代码是先新建一个表; 表明是Book
public class MySQLite extends SQLiteOpenHelper { PRivate Context context; //表 注意()前,表明和括号是有空格的,用来区分表明; public static final String CREATE_BOOK = "create table Book (id integer not null primary key autoincrement,name varchar not null,author varchar not null,price real,amount integer not null)"; public MySqLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); this.context = context; //这里记得初始化 } @Override public void onCreate(SQLiteDatabase db) { //创建sql表; db.execSQL(CREATE_BOOK); Toast.makeText(context, "数据库表创建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}2: 在学到的地方创建数据库; 例如;点击后创建 onclick()中;
//创建数据库 SQLite MySqLite mySqLite = new MySqLite(this, "BookList.db", null, 1); 号++ //通过getReadableDatabase()创建或打开数据库; mySqLite.getReadableDatabase();3:修改数据库,添加另一个表; 需使用更新的方法; 首先先要在MySqlite中写创建表的语句;
//更新数据库 //更新时新加的 public static final String CREATE_TYPE = "create table Type (id integer not null primary key autoincrement,type varchar not null)";在oncreate里:
@Override public void onCreate(SQLiteDatabase db) { //创建sql表; db.execSQL(CREATE_BOOK); db.execSQL(CREATE_TYPE);//更新时新加的 Toast.makeText(context, "数据库表创建成功", Toast.LENGTH_SHORT).show(); }3: 更新方法里:
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //更新时新加的 //利用drop来删除,这样更新内容; db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Type"); onCreate(db); }4: 最后还需要把之前的创建的地方修改一下版本号;
MySqLite mySqLite = new MySqLite(this, "BookList.db", null, 2); //更新后更改版本号++这时这个BookList.db文件中就存在两个表;
5: 增删改查 下面是使用方法 具体实现还需要自己处理,这里都写在一起了;
//通过getReadableDatabase()创建或打开数据库; SQLiteDatabase database = mySqLite.getReadableDatabase();// sqLiteDatabase.beginTransaction(); //添加数据 ContentValues values = new ContentValues(); values.put("name", "水传"); values.put("author", "小水"); values.put("price", 50); values.put("amount", 1000); long insert = database.insert("Book", null, values); if(insert>0) { Toast.makeText(DataSaveActivity.this, "水传添加成功", Toast.LENGTH_SHORT).show(); } values.clear(); // values.put("name", "梨花"); values.put("author", "小明"); values.put("price", 60); values.put("amount", 500); long insert1 = database.insert("Book", null, values); if(insert1>0) { Toast.makeText(DataSaveActivity.this, "梨花添加成功", Toast.LENGTH_SHORT).show(); } values.clear(); // //修改; values.put("author", "derM"); int update = database.update("Book", values, "name = ?", new String[]{"水传"}); if(update>0) { Toast.makeText(DataSaveActivity.this, "水传作者已修改", Toast.LENGTH_SHORT).show(); } values.clear(); //删除: int delete = database.delete("Book", "name = ?", new String[]{"梨花"}); if(delete>0) { Toast.makeText(DataSaveActivity.this, "删除梨花成功", Toast.LENGTH_SHORT).show(); }//查 Cursor cursor = database.query("Book", null, null, null, null, null, null); if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int price = cursor.getInt(cursor.getColumnIndex("price")); int amount = cursor.getInt(cursor.getColumnIndex("amount")); LogUtil.e(TAG,"name: "+name+", author: "+author+", price: "+price+", amount: "+amount); } while (cursor.moveToNext()); } cursor.close();//使用完记得关闭;这里还有数据库的事物没有说明; 事务的作用:保证数据的准确性等;如果在结束时没有执行到成功标记,则之间的所有数据库操作都会回滚,变为和之前就是一样的;如果执行到了 则数据改变;
sqLiteDatabase.beginTransaction();//这之间执行数据库操作语句;、、、、、 sqLiteDatabase.setTransactionSuccessful(); sqLiteDatabase.endTransaction();新闻热点
疑难解答