Android下数据库操作表(通过sql语句)
相关API
①插入删除修改相关操作 sqlitedatabase的 execSQL方法
②查询操作 sqlitedatabase的 rawQuery方法
③ 查询结果返回 Curor(游标 类似JDBC中的ResultSet)
curor相关方法 moveToNext() 移动到下一行
getCount() 返回查询到的结果一共有多少行
getColumnCount()返回一条结果中有多少列
getString(index), getInt(index) 根据列序号返回相应记录(序号从0开始)
代码
public void insert(View v) {SQLiteDatabase db = openHelper.getReadableDatabase();db.execSQL("insert into info(name,phone) values('赵四','13888888888')");db.execSQL("insert into info(name,phone) values('王五','13888888888')");db.close();}public void update(View v) {SQLiteDatabase db = openHelper.getReadableDatabase();db.execSQL("update info set phone = '12345678' where name='赵四' ");db.close();}public void delete(View v) {SQLiteDatabase db = openHelper.getReadableDatabase();db.execSQL("delete from info where name = '赵四'");db.close();}public void query(View v) {SQLiteDatabase db = openHelper.getReadableDatabase();Cursor cursor = db.rawQuery("select * from info where name = ? ; ", new String[]{"王五"});while(cursor.moveToNext()){for (int i = 0; i < cursor.getColumnCount(); i++) {String result = cursor.getString(i);Log.d("MainActivity", result+"");}}db.close();}
Android下数据库操作表(通过谷歌提供的api)
相关api
①insert方法
②delete方法
③update方法
④query方法
代码
public void insert1(View v) {SQLiteDatabase sqLiteDatabase = openHelper.getReadableDatabase();//--insert into info(name,phone) values('赵四','13777777');//--insert into info() values();//sqLiteDatabase.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"赵四","1388888888"});//第一个参数 表名字String table = "info";//第二个参数 避免产生插入空列的sql 如果第三个参数为空 sql会变成insert into info() values();//如果传了nullColumnHack 并且第三个参数没有传 是空的情况 sql 会拼接成如下情况: insert into info(name) values(NULL)String nullColumnHack = null;//第三个参数 通过map的形式 保存要插入的列名 和对应的值ContentValues values = new ContentValues();values.put("name", "赵四");values.put("phone", "188888888888");//最终是通过字符串拼接的形式 帮助生成sql 避免写错//result 返回值是插入行的行号 如果返回-1说明插入失败long result = sqLiteDatabase.insert(table, nullColumnHack, values);if(result>=0){Toast.makeText(this, "插入到第"+result+"行", Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "插入失败", Toast.LENGTH_SHORT).show();}sqLiteDatabase.close();}public void delete1(View v) {SQLiteDatabase sqLiteDatabase = openHelper.getReadableDatabase();//--delete from info where name='王五';//sqLiteDatabase.execSQL("delete from info where name=?;", new Object[]{"赵四"});//第二个参数 where条件 where后面的内容 具体删除的值用?代替String whereClause = "name = ? and phone = ?";//第三个参数 在where条件中 用?代替的具体的值String[] whereArgs = new String[]{"赵四","13888888888"};int num = sqLiteDatabase.delete("info", whereClause, whereArgs);if(num>0){Toast.makeText(this, "删除了"+num+"行", Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "没删,失败", Toast.LENGTH_SHORT).show();}sqLiteDatabase.close();}public void update1(View v) {SQLiteDatabase sqLiteDatabase = openHelper.getReadableDatabase();//--update info set phone='139999999'where name='王五';;//sqLiteDatabase.execSQL("update info set phone=? where name=?", new Object[]{"1399999999","赵四"});ContentValues values = new ContentValues();values.put("phone", "13999999999");String whereClause = "name = ?";String[] whereArgs = new String[]{"赵四"};int num = sqLiteDatabase.update("info", values, whereClause, whereArgs);if(num>0){Toast.makeText(this, "更新了"+num+"行", Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "没更新,失败", Toast.LENGTH_SHORT).show();}sqLiteDatabase.close();}public void query1(View v) {SQLiteDatabase sqLiteDatabase = openHelper.getReadableDatabase();//--select name,phone from info;;//Cursor cursor = sqLiteDatabase.rawQuery("select * from info", null);String[] columns = null;//要查询的列名字 如果传null 相当于select * from info;String selection = null; //查询的条件 其中又变化的部分要用? 替换String[] selectionArgs = null; //查询条件中 ? 所对应的值String groupBy = null;//分组String having = null;//分组接的条件String orderBy = null;//排序Cursor cursor = sqLiteDatabase.query("info", columns, selection, selectionArgs, groupBy, having, orderBy);if(cursor!=null && cursor.getCount()>0){while(cursor.moveToNext()){String name = cursor.getString(1);String phone = cursor.getString(cursor.getColumnIndex("phone"));Log.e("sqlite", "name==="+name+"phone==="+phone);}}cursor.close();sqLiteDatabase.close();}
对比
| 大量数据效率 | SQL语句灵活性 | 方法是否有返回值 | SQL出错概率 | |
| 通过sql执行CRUD | 效率高 | 灵活性强任意发挥 | 除查询外无 | 有可能出错 |
| 通过api执行CRUD | 需要拼语句效率低 | 受api限制 | 都有返回值 | 不会出错 |