保存数据(3)sqlLite保存数据
发布时间:2020-12-12 20:40:19 所属栏目:百科 来源:网络整理
导读:1 内存数据库存储 String createTableSQL = "CREATE TABLE [t_test] (" + "[id] INTEGER,"+ "[name] VARCHAR(20),[memo] TEXT,"+ "CONSTRAINT [sqlite_autoindex_t_test_1] PRIMARY KEY ([id]))";SQLiteDatabase sqLiteDatabase = SQLiteDatabase.create(nul
1 内存数据库存储
String createTableSQL = "CREATE TABLE [t_test] (" + "[id] INTEGER," + "[name] VARCHAR(20),[memo] TEXT," + "CONSTRAINT [sqlite_autoindex_t_test_1] PRIMARY KEY ([id]))"; SQLiteDatabase sqLiteDatabase = SQLiteDatabase.create(null); sqLiteDatabase.execSQL(createTableSQL); String insertSQL = "insert into t_test(id,name) values(?,?)"; sqLiteDatabase.execSQL(insertSQL,new Object[] { 1,"老毕" }); String selectSQL = "select name from t_test"; Cursor cursor = sqLiteDatabase.rawQuery(selectSQL,null); cursor.moveToFirst(); Toast.makeText(this,cursor.getString(0),Toast.LENGTH_LONG).show(); sqLiteDatabase.close(); 2 物理文件数据库存储 String filename = android.os.Environment.getExternalStorageDirectory() + "/sdcard_test.db"; String createTableSQL = "CREATE TABLE [t_test] (" + "[id] INTEGER," + "CONSTRAINT [sqlite_autoindex_t_test_1] PRIMARY KEY ([id]))"; File file = new File(filename); if (file.exists()) { file.delete(); } SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(filename,null); database.execSQL(createTableSQL); ContentValues contentValues = new ContentValues(); contentValues.put("id",1); contentValues.put("name","Mike"); contentValues.put("memo","Student"); database.insert("t_test",null,contentValues); String insertSQL = "insert into t_test(id,name,memo) values(?,?,?)"; database.execSQL(insertSQL,new Object[]{2,"John","???"}); String selectSQL = "select name,memo from t_test where name=?"; Cursor cursor = database.rawQuery(selectSQL,new String[] { "John" }); cursor.moveToFirst(); Toast.makeText(this,cursor.getString(0) + " " + cursor.getString(1),Toast.LENGTH_LONG).show(); database.close(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |