2018年5月15日临下班前找的一个读取assets下数据库的例子

网页   https://blog.csdn.net/li12412414/article/details/51958774

  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.           
  5.         //打开或创建test.db数据库  
  6.         SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);  
  7.         db.execSQL("DROP TABLE IF EXISTS person");  
  8.         //创建person表  
  9.         db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");  
  10.         Person person = new Person();  
  11.         person.name = "john";  
  12.         person.age = 30;  
  13.         //插入数据  
  14.         db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[]{person.name, person.age});  
  15.           
  16.         person.name = "david";  
  17.         person.age = 33;  
  18.         //ContentValues以键值对的形式存放数据  
  19.         ContentValues cv = new ContentValues();  
  20.         cv.put("name", person.name);  
  21.         cv.put("age", person.age);  
  22.         //插入ContentValues中的数据  
  23.         db.insert("person", null, cv);  
  24.           
  25.         cv = new ContentValues();  
  26.         cv.put("age", 35);  
  27.         //更新数据  
  28.         db.update("person", cv, "name = ?", new String[]{"john"});  
  29.           
  30.         Cursor c = db.rawQuery("SELECT * FROM person WHERE age >= ?", new String[]{"33"});  
  31.         while (c.moveToNext()) {  
  32.             int _id = c.getInt(c.getColumnIndex("_id"));  
  33.             String name = c.getString(c.getColumnIndex("name"));  
  34.             int age = c.getInt(c.getColumnIndex("age"));  
  35.             Log.i("db", "_id=>" + _id + ", name=>" + name + ", age=>" + age);  
  36.         }  
  37.         c.close();  
  38.           
  39.         //删除数据  
  40.         db.delete("person", "age < ?", new String[]{"35"});  
  41.           
  42.         //关闭当前数据库  
  43.         db.close();  
  44.           
  45.         //删除test.db数据库  
  46. //      deleteDatabase("test.db");  
  47.     }  
原文地址:https://www.cnblogs.com/strongdady/p/9041906.html