使用Android进行高效的SQLite插入,可以通过以下几个步骤来实现:
以下是一个简单的示例代码:
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 MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在主函数中插入数据:
MyDatabaseHelper dbHelper = new MyDatabaseHelper(getApplicationContext(), "BookStore.db", null, 1);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "Android高效插入SQLite");
values.put("author", "张三");
values.put("price", 89.8);
values.put("pages", 380);
db.insert("Book", null, values);
db.close();
这样就可以在Android中高效地使用SQLite进行插入操作了。
领取专属 10元无门槛券
手把手带您无忧上云