首页 > 代码库 > SQlite使用
SQlite使用
SQlite是android默认的数据库,属于轻量型的关系型数据库。用法如下:
DBHelper类:
public class MyDBHelper extends SQLiteOpenHelper{ public static final String DB_NAME = "dbtest1.db"; public static final String TABLE_NAME = "table1"; public MyDBHelper(Context context) { super(context, DB_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { String sql0= "drop table if exists " + TABLE_NAME; db.execSQL(sql0); String sql = "create table if not exists " + TABLE_NAME + " (id integer primary key, name text, age integer, address text)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "drop table if exists " + TABLE_NAME; db.execSQL(sql); onCreate(db); } }
具体使用方式:
public class DbTest { private static MyDBHelper dbHelper = null; private static void createTable(Context context){ if(dbHelper == null){ dbHelper = new MyDBHelper(context); } } private static void insertTest(){ SQLiteDatabase db = dbHelper.getWritableDatabase(); db.beginTransaction(); db.execSQL("insert into " + MyDBHelper.TABLE_NAME + " (id, name, age, address) values(1, ‘Jack‘, 25, ‘beijing‘ )"); db.execSQL("insert into " + MyDBHelper.TABLE_NAME + " (id, name, age, address) values(2, ‘Green‘, 26, ‘shanghai‘ )"); db.execSQL("insert into " + MyDBHelper.TABLE_NAME + " (id, name, age, address) values(3, ‘Herry‘, 27, ‘nanjing‘ )"); // db.setTransactionSuccessful(); // db.beginTransaction(); ContentValues values = new ContentValues(); values.put("id", 4); values.put("name", "Mary"); values.put("age", 28); values.put("address", "shenzhen"); db.insertOrThrow(MyDBHelper.TABLE_NAME, null, values); db.setTransactionSuccessful(); db.endTransaction(); } private static void queryTest(){ SQLiteDatabase db = dbHelper.getWritableDatabase(); String[] colums = new String[]{"id", "name", "age", "address"}; Cursor cursor = db.query(MyDBHelper.TABLE_NAME, colums, null, null, null, null, null); if(cursor!=null && cursor.getCount()>0){ while(cursor.moveToNext()){ int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); String address = cursor.getString(cursor.getColumnIndex("address")); Log.i(MainActivity.TAG, "(" + id + ", " + name + ", " + age + ", " + address + ")"); } } cursor.close(); } private static void deleteTest(){ SQLiteDatabase db = dbHelper.getWritableDatabase(); // String sql = "delete from " + MyDBHelper.TABLE_NAME + " where name = ‘Green‘"; // db.execSQL(sql); db.beginTransaction(); db.delete(MyDBHelper.TABLE_NAME, "name=?", new String[]{"Green"}); db.setTransactionSuccessful(); // db.endTransaction(); } private static void updateTest(){ SQLiteDatabase db = dbHelper.getWritableDatabase(); // String sql = "update " + MyDBHelper.TABLE_NAME + " set name=‘change‘ where age=27"; // db.execSQL(sql); db.beginTransaction(); ContentValues values = new ContentValues(); values.put("name", "change1"); db.update(MyDBHelper.TABLE_NAME, values, "age=?", new String[]{"27"}); db.setTransactionSuccessful(); db.endTransaction(); } public static void dbTest(Context context){ createTable(context); insertTest(); queryTest(); // deleteTest(); // queryTest(); updateTest(); queryTest(); } }
SQlite使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。