对腾讯api的学习已经告一段落,具体到时用的到再去研究,基本大体上已经能够运用了,今天突然心血来潮,想学习一下android数据库,看了mars老师的视频,大约花了一个小时的时间,我感觉已经接触到了门槛了,下面是我学习的内容:
1.通过SQLiteOpenHelper对数据库进行操作
看了一下api文档对其的描述:
(1)它是一个抽象类
public abstract class
SQLiteOpenHelper
extends Object
(2)A helper class to manage database creation and version management.
它是一个管理类,控制数据库的构建和版本更新
(3)You create a subclass implementing onCreate(SQLiteDatabase)
, onUpgrade(SQLiteDatabase, int, int)
and optionally onOpen(SQLiteDatabase)
, and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.
This class makes it easy for ContentProvider
implementations to defer opening and upgrading the database until first use, to avoid blocking application startup with long-running database upgrades.
要继承这个类必须实现它的Oncreate()和OnUpgrade()方法
(4)下面来构造一个这样的类
public class SqliteDateBaseHelper extends SQLiteOpenHelper { public static final int VERSION = 1; /** * 构造方法 * * @param context * : 对应的上下文 * @param name * :数据库名字 * @param factory * : to use for creating cursor objects, or null for the default * @param version * : 版本 Create a helper object to create, open, and/or manage a * database. This method always returns very quickly. The * database is not actually created or opened until one of * getWritableDatabase() or getReadableDatabase() is called. */ public SqliteDateBaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, null, version); // TODO Auto-generated constructor stub } public SqliteDateBaseHelper(Context context, String name, int version) { super(context, name, null, version); // TODO Auto-generated constructor stub } public SqliteDateBaseHelper(Context context, String name) { super(context, name, null, VERSION); // TODO Auto-generated constructor stub } /** * 创建数据库 */ @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub //创建一个表 db.execSQL("create table userinfo(id int,username varchar(20),userpass varchar(20))"); System.out.println("---->创建数据库"); } /** * 更新数据库版本 */ @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub System.out.println("---->更新数据库"); } }
(5)创建一个Activity来展示效果
public class SqliteActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.button1:// 创建数据库
SqliteDateBaseHelper dateBaseHelper = new SqliteDateBaseHelper(
SqliteActivity.this, "mydatebase");
// 创建数据库实例
SQLiteDatabase db = dateBaseHelper.getReadableDatabase();
break;
case R.id.button2:// 更新数据库版本
SqliteDateBaseHelper dateBaseHelper1 = new SqliteDateBaseHelper(
SqliteActivity.this, "mydatebase", 2);
SQLiteDatabase db1 = dateBaseHelper1.getReadableDatabase();
break;
case R.id.button3:// 插入数据
ContentValues contentValues = new ContentValues();
contentValues.put("id", 1);
contentValues.put("username", "AMB");
contentValues.put("userpass", "88520");
SqliteDateBaseHelper dateBaseHelper2 = new SqliteDateBaseHelper(
SqliteActivity.this, "mydatebase", 2);
SQLiteDatabase db2 = dateBaseHelper2.getWritableDatabase();
db2.insert("userinfo", null, contentValues);
break;
case R.id.button4:// 更新数据
SqliteDateBaseHelper dateBaseHelper3 = new SqliteDateBaseHelper(
SqliteActivity.this, "mydatebase", 2);
SQLiteDatabase db3 = dateBaseHelper3.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", "再见_孙悟空");
db3.update("userinfo", values, "id=?", new String[] { "1" });
break;
case R.id.button5:// 查找数据
SqliteDateBaseHelper dateBaseHelper4 = new SqliteDateBaseHelper(
SqliteActivity.this, "mydatebase", 2);
SQLiteDatabase db4 = dateBaseHelper4.getReadableDatabase();
Cursor cursor = db4.query("userinfo", new String[] { "id",
"username", "userpass" }, "id=?", new String[] { "1" },
null, null, null);
while (cursor.moveToNext()) {
String username = cursor.getString(cursor
.getColumnIndex("username"));
System.out.println(username);
}
break;
}
}
}
这里要注意的就是The
* database is not actually created or opened until one of
* getWritableDatabase() or getReadableDatabase() is called.
要调用getWritableDatabase() or getReadableDatabase()后数据库才被真正的创建
首先打开cmd
然后输入adb,如果显示是什么没找到之类的话,那就是还没配置adb
那么可以参考http://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html
配置好了的话,输入adb shell进入根目录(必须要再开启模拟器之后)
然后 输入ls -l查看文件夹
再通过ls -l查看,
再ls -l 查看一下:找到你这个项目的包名
看到了databases,本来是没有的,按了第一个按钮后才创建的
切进去,看到了数据库contact 和 mydatebase
进去后调用.schema查看表信息