1.SQLite介绍
官方网站:http://www.sqlite.org/
2.SQLiteOpenHelper使用方法
getReadableDatabase() //创建或打开一个只读的数据库
getWriteableDatabase() //创建或打开一个可读写的数据库
onCreate(SQLiteDatabase db) //创建数据库
onOpen(SQLiteDatabase db) //打开数据库
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) //更新数据库
close() //关闭数据库
3.在SQLOpenHelper的子类中,必须有该构造函数
public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
public class DatabaseHelper extends SQLiteOpenHelper { // 默认版本 private static final int VERSION = 1; // 默认构造函数 public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } // 构造函数(用于创建数据库) public DatabaseHelper(Context context, String name) { this(context, name, null, VERSION); } // 构造函数(用于更新数据库) public DatabaseHelper(Context context, String name, int version) { this(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("onCreate is start..."); db.execSQL("create table user( id int, name varchar(20) )"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("onUpgrade is start..."); } }
4.使用adb访问SQLite
adb shell //进行linux命令行(调试环境)
ls -l //相当于dir /w
cd data //与cd相同
sqlite3 dbname //进入SQL管理模式
.schema //查看数据库中的表
SQL语句 //可直接查询
rm -r databases //循环删除目录
5.增、删、改、查
public class SQLiteActivity extends Activity { // 声明控件 private Button btnCreate = null; private Button btnUpgrade = null; private Button btnInsert = null; private Button btnUpdate = null; private Button btnDelete = null; private Button btnSelect = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sqlite); // 寻找控件 btnCreate = (Button) findViewById(R.id.btnCreateDb); btnUpgrade = (Button) findViewById(R.id.btnUpgradeDb); btnInsert = (Button) findViewById(R.id.btnInsertDb); btnUpdate = (Button) findViewById(R.id.btnUpdateDb); btnDelete = (Button) findViewById(R.id.btnDeleteDb); btnSelect = (Button) findViewById(R.id.btnSelectDb); // 创建数据库 btnCreate.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("btnCreate on start..."); DatabaseHelper helper = new DatabaseHelper(SQLiteActivity.this, "studyDb"); helper.getReadableDatabase(); } }); // 更新数据库 btnUpgrade.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("btnUpgrade on start..."); DatabaseHelper helper = new DatabaseHelper(SQLiteActivity.this, "studyDb", 2); helper.getReadableDatabase(); } }); // 新增数据 btnInsert.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("btnInsert on start..."); DatabaseHelper helper = new DatabaseHelper(SQLiteActivity.this, "studyDb"); SQLiteDatabase db = helper.getWritableDatabase(); // 参数列表 ContentValues params = new ContentValues(); params.put("id", 1); params.put("name", "dier"); // 表名、列名、参数列表 db.insert("user", null, params); } }); // 修改数据 btnUpdate.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("btnUpdate on start..."); DatabaseHelper helper = new DatabaseHelper(SQLiteActivity.this, "studyDb"); SQLiteDatabase db = helper.getWritableDatabase(); // 参数列表 ContentValues params = new ContentValues(); params.put("name", "stdier"); // 表名、参数列表、WHERE子句、子句参数 db.update("user", params, "id=?", new String[] { "1" }); } }); // 删除数据 btnDelete.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("btnDelete on start..."); DatabaseHelper helper = new DatabaseHelper(SQLiteActivity.this, "studyDb"); SQLiteDatabase db = helper.getWritableDatabase(); // 表名、列名、参数列表 db.delete("user", "id=?", new String[] { "1" }); } }); // 修改数据 btnSelect.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("btnSelect on start..."); DatabaseHelper helper = new DatabaseHelper(SQLiteActivity.this, "studyDb"); SQLiteDatabase db = helper.getWritableDatabase(); //表名、查询字段、WHERE子句、子句参数、groupBy、having、orderBy Cursor cursor = db.query("user", new String[] { "id", "name" }, "id=?", new String[] { "1" }, null, null, null); while (cursor.moveToNext()) { System.out.println("query name -> " + cursor.getString(cursor.getColumnIndex("name"))); } } }); } }