Android下SQLite数据库学习笔记1——SQLite数据库的使用

SQLite数据库

  • 移动平台的嵌入式关系型数据库
    • 所以他所支持的数据类型就比较少,NULL,INTEGER,REAL(浮点数字),TEXT(字符串文本),BLOB(二进制对象)这五种,但实际上,SQLite3也接受varchar(n),char(n),等数据类型,只不过在运算或保存的时候,会转换成对应的五中数据类型。
    • 最大的特点:你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么,
    • 注意:定义为主键的字段只能存储64位的整数,当保存其他类型字段的时候,会报错
    • SQLite在解析CREATE TABLE语句时,会忽略语句中跟在字段后面的数据类型信息,写上只是为了代码易于理解。

SQLiteOpenHelper

  • 数据库创建于打开的帮助类,需要创建实现类
  • 实现类需要实现的方法
  • 构造方法
public PersonSQLiteOpenHelper(Context context, String name,CursorFactory factory, int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
  • 参数介绍:
    • context:to use to open or create the database(上下文,告诉程序,数据库创建在那个目录下)
    • name:of the database file, or null for an in-memory database(数据库文件的名字)
    • factory:to use for creating cursor objects, or null for the default(用来创建游标对象,可以设为null,使用默认的结果集的指针。游标工厂)
    • version:number of the database (starting at 1); if the database is older, onUpgrade will be used to upgrade the database; if the database is newer, onDowngrade will be used to downgrade the database(数据库的版本,最小是从1开始)
  • onCreate方法(需要重写父类的方法)
@Override
public void onCreate(SQLiteDatabase db) {
    // 初始化数据库的表结构
    db.execSQL("create table person (id integer primary key autoincrement, name varchar(20), number varchar(20))");
}

Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.(当数据库第一次被调用的时候被创建,在此定义数据库的表结构,和初始化数据)

  • onUpgrade方法
@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

Called when the database needs to be upgraded.(当数据库需要更新的时候调用)

  • 创建数据库
    • new出数据库帮助类对象后只有调用getWritableDatabase()或者getReadableDatabase()方法,数据库才会被创建
  • 创建测试:
    • 另建一个专门测试的包,新建测试类,继承AndroidTestCase类(介绍:Extend this if you need to access Resources or other things that depend on Activity Context)
    • 新建方法
public class TestPersonDB extends AndroidTestCase {

    public void testCreateDB() throws Exception {
        PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(getContext());
        helper.getWritableDatabase();
    }

}

帮助类可以通过框架提供的getContext来获取context

  • 此时如果直接直接对这个方法Run AS Android JUnit Test会报错

    数据库 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

    • 即在这个项目里的AndroidManifest.xml文件里缺少instrumenttation,和uses-library

    解决方法:在AndroidManifest.xml文件里配置:
    1.<instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.jxust.db" />
    2.把<uses-library android:name="android.test.runner" />配在<application></applocation>之间

    AndroidManifest.xml文件配置

  • 这时就可以对这个方法Run AS>Android JUnit Test,成功。
  • 将项目部署在模拟器上后,在data/data/com.jxust.db文件夹下,可以发现多了一个database文件夹,哪里有我们新建的数据库文件
    数据库
  • 导出文件用SQLite Expert软件查看数据库的结构,发现除了我们建的表,还多了一个”android_metadata”表。
    android_metadata表
  • 这是由系统自动创建出来的,只有一个内容locale这时google攻城狮为了方便以后做扩展而定义出来的表,希望对数据库也能国际化
    SQLiteExpertPers下载地址
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值