您的位置:首页 > 移动开发 > Android开发

Android SQLite

2016-07-24 11:39 507 查看
adb shell 调试

(1)配置环境
系统变量
新建android变量
将android开发工具目录加;再加上sdk下的platform-tools目录
编辑path变量
内容最后加;再加上android变量中的全部内容
(2)检测是否配置成功
Win+R
cmd
键入adb,不显示为外部命令则配置成功


adb shell 调入SQLite

Win+R
cmd
键入adb
键入adb shell
列出文件列表含有详细权限信息:ls -l
列出文件列表:ls
进入文件夹:cd 文件夹名称
进入SQLite数据库:sqlite3 数据库名称,命令由 # 变为sqlite>
循环删除一个目录里的所有内容: rm -r 目录名称
example:
cd data
cd data
cd demo.engineerzhong.com.sqlitedemo
ls
cd databases
sqlite3 Kiku_db
select * from userInfo;


SQLite 增删改查

查询原型:
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs,String groupBy, String having,String orderBy,String limit)

Explain: Query the given table, returning a Cursor over the result set.
Example:
事先建好SQLiteHelper辅助类
SQLiteHelper db = new SQLiteHelper(MainActivity.this,"Kiku_db");
SQLiteDataBase dbtest = db.getReadableDatabase();
Cursor cursor = dbtest.query("userInfo",new String[]{"id","name"},"id=?",new String[]{“2”},null,null,null,null);
//查询语句等同于: select * from userInfo where id = 2;
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(MainActivity.this,"id: " + id + " name: "+name,Toast.LENGTH_SHORT).show();
`}
dbtest.close();

插入数据原型
public long insert (String table, String nullColumnHack, ContentValues values)

Explain: Convenience method for inserting a row into the database.
Example:
事先建好SQLiteHelper辅助类
SQLiteHelper db = new SQLiteHelper(MainActivity.this,"Kiku_db");
SQLiteDataBase dbtest = db.getReadableDatabase();
ContentValues value = new ContentValues();
value.put("id",1);
value.put("name","Engingeer");
dbtest.insert(“userInfo”,null,value);
//等同于SQLite插入语句 insert into userInfo(1,"Engineer");
Toast.makeText(MainActivity.this,"插入数据成功",Toast.LENGTH_SHORT).show();
dbtest.close();

修改数据原型:
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
Explain: Convenience method for updating rows in the database.
Example:
事先建好SQLiteHelper辅助类
SQLiteHelper db = new SQLiteHelper(MainActivity.this,"Kiku_db");
SQLiteDataBase dbtest = db.getReadableDatabase();
ContentValues value = new ContentValues();
value.put("id",1);
value.put("name","EngineerZhong");
int i = dbtest.update("userInfo",value,"id=?",new String[]{"1"});
//等同于SQLite里修改语句 update userInfo set id = 1,name="EngineerZhong",where id = 2;
if( i == 1){
Toast.makeText(MainActivity.this,"修改数据成功",Toast.LENGTH.SHORT).show();
}else{
Toast.makeText(MainActivity.this,"修改数据失败",Toast.LENGTH.SHORT).show();
}
dbtest.close();
删除数据原型:
public int delete (String table, String whereClause, String[] whereArgs)
Explain: Convenience method for deleting rows in the database.
Example:
事先建好SQLiteHelper辅助类
SQLiteHelper db = new SQLiteHelper(MainActivity.this,"Kiku_db");
SQLiteDataBase dbtest = db.getReadableDatabase();
int i = dbtest.delete("userInfo","id=?",new String[]{"1"});
//等同于SQLite中删除语句 delete from userInfo where id = 1;
//   ? 是占位符 类似于JDBC
if( i == 1){
Toast.makeText(MainActivity.this,"修改数据成功",Toast.LENGTH.SHORT).show();
}else{
Toast.makeText(MainActivity.this,"修改数据失败",Toast.LENGTH.SHORT).show();
}
dbtest.close();


更新时间:2016年7月30日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android