sqliteDatabase遵循sqlite3规范。除了对数据库的事务进行操作。执行sql语句,还封装了CURD四个类型的方法。
@H_404_6@sqlite3官网:http://www.sqlite.org/
创建简单的CURD例子:(对于数据库操作不需要配置任何AndroidManifest.xml权限)
/** * @author Lean */ public class FirstDbOperation { private Context mContext; private final String DB_NAME="MyFirstDbStorage"; private final String TABLE_NAME = "'user_info_tab'"; public static String COLUMN_USERNAME="userName"; public static final String COLUMN_USERADDRESS = "userAddress"; public FirstDbOperation(Context context){ this.mContext=context; } public sqliteDatabase OpenOrCreateDBStorage(){ sqliteDatabase sqlDb=mContext.openOrCreateDatabase(DB_NAME,Context.MODE_PRIVATE,null); String sql="create table if not exists "+TABLE_NAME+ " ("+COLUMN_USERNAME+" vachar(50),"+COLUMN_USERADDRESS+" vachar(50));"; sqlDb.execsql(sql); return sqlDb; } public void insertData(sqliteDatabase sqlDb){ if (sqlDb!=null) { ContentValues values=new ContentValues(); values.put(COLUMN_USERNAME,"Lean"); values.put(COLUMN_USERADDRESS,"longdong"); sqlDb.insert(TABLE_NAME,"",values); } } public Cursor queryData(sqliteDatabase sqlDb){ if (sqlDb!=null) { //String[] columns 该列在cursorAdapter中使用时如果不为null,必须加上 _id return sqlDb.query(TABLE_NAME,new String[]{COLUMN_USERNAME},null,null); } return null; } /** * @return the number of rows affected */ public int deleteData(sqliteDatabase sqlDb){ String whereClause=COLUMN_USERNAME+"=?"; if (sqlDb!=null) { return sqlDb.delete(TABLE_NAME,whereClause,new String[]{"Lean"}); } return 0; } /** * @return the number of rows affected */ public int updateData(sqliteDatabase sqlDb){ String whereClause=COLUMN_USERNAME+"=?"; ContentValues values=new ContentValues(); values.put(COLUMN_USERNAME,"newUserName"); if (sqlDb!=null) { return sqlDb.update(TABLE_NAME,values,new String[]{"Lean"}); } return 0; } }
调用如下:
/** * @author Administrator * */ public class MainActivity extends Activity { private ServiceConnection mConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } private void initUI() { findViewById(R.id.insertDbData).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { putDataToUserTab(); } }); findViewById(R.id.queryDbData).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { queryDataFromUserTab(); } }); findViewById(R.id.deleteDbData).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { deleteDataFromUserTab(); } }); findViewById(R.id.updateDbData).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { updateDataFromUserTab(); } }); } private void putDataToUserTab() { FirstDbOperation dbOperation=new FirstDbOperation(this); dbOperation.insertData(dbOperation.OpenOrCreateDBStorage()); } private void queryDataFromUserTab() { FirstDbOperation dbOperation=new FirstDbOperation(this); Cursor cursor=dbOperation.queryData(dbOperation.OpenOrCreateDBStorage()); if (cursor!=null&&cursor.moveToFirst()) { Log.v("log",cursor.getString(cursor.getColumnIndex(FirstDbOperation.COLUMN_USERNAME))); while (cursor.moveToNext()) { Log.v("log",cursor.getString(cursor.getColumnIndex(FirstDbOperation.COLUMN_USERNAME))); } } } private void deleteDataFromUserTab() { FirstDbOperation dbOperation=new FirstDbOperation(this); int effectedLenth=dbOperation.deleteData(dbOperation.OpenOrCreateDBStorage()); Log.v("log","effectedLenth:"+effectedLenth); } private void updateDataFromUserTab() { FirstDbOperation dbOperation=new FirstDbOperation(this); int effectedLenth=dbOperation.updateData(dbOperation.OpenOrCreateDBStorage()); Log.v("log","effectedLenth:"+effectedLenth); } }查看数据库信息:
>>adb shell
>>su
>>chmod 777 /data/data /data/data/package_name/data/data/package_name/* /data/data/package_name/databases/*
原文链接:https://www.f2er.com/sqlite/200547.html