标签:
本章内容
第1节 SQLite数据库概述
第2节 SQLite建库建表
第3节 管理数据库连接
第4节 操作数据库数据
第5节 数据绑定
本章目标
掌握SQLite数据的基本特点与工具使用。
熟练掌握SQLite建库建表的方法。
熟练掌握连接SQLite数据库的方法。
熟悉SQLite数据库的升级与建立方法。
掌握通过数据绑定完成数据显示的方法。
SQLite数据库简介
http://www.sqlite.org/download.html
$ sqlite3 <数据库文件路径>
$ sqlite3 test.db
$ sqlite3 test.db <sql.script
为了与其他数据库兼容,可以为字段指定默认的类型
NULL:空值
INTEGER: 带符号的整数,具体取决于存入数字的范围大小
REAL:浮点数,存储为8-bytes的浮点数
TEXT:字符串文本
BLOB:二进制对象
同时还接受如下一些类型:
smallint 16位整数
int 32位整数
float 32位浮点数
double 64位浮点数
char(n) n不能炒作254
varchar(n) n不能超过4000
date
time
limestamp
create table books (
id integer primary key autoincrement,
name varchar(128) not null unique,
author varchar(128) not null,
price double not null
);
create table groups(
id integer primary key autoincrement,
name varchar(128) not null unique
);
crate table users(
id integer primary key autoincrement,
group_id integer constraint fk_users_group_id references groups(id),
username varchar(128) not null
password varchar(128) not null
);
sqlite> begin; sqlite> insert into …… sqlite> commit; sqlite> rollabck;
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
public class MyHelper extends SQLiteOpenHelper {
public static final int VERSION = 1;
public static final String DATABASE_NAME = “test.db”;
public MyHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
}
SQLiteDatabase db = helper.getWritableDatabase();
public MyHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
public void onCreate(SQLiteDatabase db) {
String str_sql = "CREATE TABLE " + TABLE_NAME
+ "(” + ID
+ " INTEGER PRIMARY KEYAUTOINCREMENT,”
+ TEXT + " text);";
db.execSQL(str_sql);
} public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
//这里填写数据库升级操作的代码
}
execSQL
insert、insertOrThrow、insertWithOnConflict
query、rawQuery
replace、replaceOrThrow
update、updateWithOnConflict
delete
//将一条新记录的各个字段内容装入一个ContentValues对象
ContentValues cv = new ContentValues();
cv.put("name",user.getName());
cv.put("age",user.getAge());
cv.put("remark",user.getRemark());
//插入一条新记录
db.insert("users",null, cv);//第一个参数为表名
//第二个参数表示where后的条件表达式,可以使用?
//第三个参数则是一个对应每一个?值的数组
db.delete("users", "id=?", new String[]{String.valueOf(userId)});
ContentValues cv = new ContentValues();
cv.put("name", user.getName());
cv.put("age", user.getAge());
cv.put("remark", user.getRemark());
db.update("users", cv, "id=?",
new String[]{String.valueOf(userId)});
Cursor c = db.query("users", null, null, null, null, null, "name");
List<User> users = null;
if(c != null) {
users = new ArrayList<User>();
while(c!=null && c.moveToNext()) {
User u = new User();
u.setId(c.getInt(0));
u.setName(c.getString(1));
u.setAge(c.getInt(2));
u.setRemark(c.getString(3));
users.add(u);
}
c.close();
}
Cursor c = db.query(
“users”, //表名
new String[]{“name”, “age”}, //select包含的字段
“age > ?”, //where条件表达式
new String[]{“10”}, //条件值
null, //group子句
null, //having子句
“name desc” //排序字段
);
String sql = “select name, age from users where age > ?”
Cursor c = db.query(
sql
new String[]{“10”}
);
db.beginTransaction(); //开始事务
try {
…… //这里填写数据库操作代码
db.setTransactionSuccessful(); //提交事务
} finally {
db.endTransaction(); //关闭事务
}
数据绑定的必要性
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/nametextview“ android:layout_width="0dp“
android:layout_height="40dp“ android:layout_weight="1"/>
<TextView
android:id="@+id/agetextview“ android:layout_width="80dp"
android:layout_height="40dp"/>
</LinearLayout>
ListView bookListView = (ListView)findViewById(R.id.booklist);
String [] from = new String[] { "_name", "_age“ };
int [] to = new int[] { R.id.nametextview, R.id.agetextview };
Cursor cursor = db.rawQuery(“select * from books”, null);
SimpleCursorAdapter adapter = new
SimpleCursorAdapter(MainActivity.this,
R.layout.book_list_item, cursor, from, to, 0);
bookListView.setAdapter(adapter);
SimpleCursorAdapter.ViewBinder viewBinder = new
SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
if(cursor.getColumnIndex("_name") == columnIndex) {
TextView v = (TextView)view;
v.setText("N:" + cursor.getString(columnIndex));
return true;
}
return false;
}
};
ListView bookListView = (ListView)findViewById(R.id.booklist);
String [] from = new String[] { "_name", "_age“ };
int [] to = new int[] { R.id.nametextview, R.id.agetextview };
Cursor cursor = db.rawQuery(“select * from books”, null);
SimpleCursorAdapter adapter = new
SimpleCursorAdapter(MainActivity.this,
R.layout.book_list_item, cursor, from, to, 0);
adapter.setViewBinder(viewBinder);
bookListView.setAdapter(adapter);
标签:
原文地址:http://blog.csdn.net/zhangchen124/article/details/51894294