ContentProvider-SqliteDatabase实现增删查改(1)

ContentProvider-SqliteDatabase实现增删查改(1),第1张

概述SqliteDatabase遵循sqlite3规范。除了对数据库的事务进行 *** 作。执行sql语句,还封装了CURD四个类型的方法。 sqlite3官网:http://www.sqlite.org/ 创建简单的CURD例子:(对于数据库 *** 作不需要配置任何AndroidManifest.xml权限) /** * @author Lean */public class FirstDbOperatio

sqliteDatabase遵循sqlite3规范。除了对数据库的事务进行 *** 作。执行SQL语句,还封装了CURD四个类型的方法。

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);	}}
查看数据库信息:

1.root手机权限;
2.修改数据库DOCS命令:

>>adb shell

>>su

>>chmod 777 /data/data /data/data/package_name/data/data/package_name/* /data/data/package_name/databases/*

3.导出数据库文件;

4.安装并拖动文件到sqlite expert professional;

总结

以上是内存溢出为你收集整理的ContentProvider-SqliteDatabase实现增删查改(1)全部内容,希望文章能够帮你解决ContentProvider-SqliteDatabase实现增删查改(1)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1172437.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存