Android中使用SQLite数据库要通过SQLiteOpenHelper类。

首先,定义相关变量:

// 数据库变量DatabaseHelper mDBH;SQLiteDatabase db;public static String strSql;

再定义SQLiteOpenHelper类:

public static class DatabaseHelper extends SQLiteOpenHelper{public static final String DATABASE_NAME = "Call_db.db";public static final int DATABASE_VERSION = 1;public static final String TABLE_NAME = "Call";public static final String TABLE_NAME_2 = "Days";public static final String NAME = "Name";public static final String NUMBER = "Number";public static final String DATE = "Date";public static final String DATES = "Dates";public static final String YEAR = "Year";public static final String MONTH = "Month";public static final String DAY = "Day";public static final String HOUR = "Hour";public static final String MINUTE = "Minute";public static final String DOW = "Dow";public static final String TYPE = "Type";public static final String INC = "InC";public static final String OUTC = "OutC";public static final String TOTAL = "Total";DatabaseHelper(Context context){super(context, DATABASE_NAME, null, DATABASE_VERSION);}// 没有数据库时,建立数据库@Overridepublic void onCreate(SQLiteDatabase db) {strSql = "CREATE TABLE " + TABLE_NAME + " (" + NAME+ " text not null, " + NUMBER + " text not null, " + DATE + " text not null, " + DATES+ " text not null, "+ YEAR + " text not null, " + MONTH + " text not null, " + DAY + " text not null, " + HOUR + " text not null, " + MINUTE + " text not null, " + DOW  + " text not null, " + TYPE + " text not null" + ");";db.execSQL(strSql);strSql = "CREATE TABLE " + TABLE_NAME_2 + " (" + DATES+ " text not null, " + YEAR + " text not null, " + MONTH + " text not null, " + DAY+ " text not null, "+ DOW + " text not null, " + INC + " text not null, " + OUTC + " text not null, " + TOTAL + " text not null);";db.execSQL(strSql);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}

当数据库不存在时(如第一次运行时),调用onCreate(SQLiteDatabase db)建立数据库,建立的方法是定义SQL语句,再执行该语句。

更多相关文章

  1. Android定制--------自定义关机时间
  2. android SQLite操作
  3. android SQLite操作
  4. 【Gradle】自定义Android(安卓)Gradle工程
  5. Android应用程序绑定服务(bindService)的过程源代码分析(2)
  6. android中滑动SQLite数据库分页加载
  7. ListView取消和自定义分割线的方法
  8. 【Gradle】自定义Android(安卓)Gradle工程
  9. 海创软件组--20200712--Butterknife与自定义图标字体

随机推荐

  1. Android 1.5 1.6 2.0 2.1 2.2 2.3 3.0,4.
  2. 使用观察者模式来实现webview的设置
  3. 开始使用Android和Kotlin
  4. Android4.0下的Calendar、events、remind
  5. eclipse 上调试android的自带应用方法 一
  6. Android(安卓)Activity与Service通信
  7. Android提取系统所有的缩略图
  8. Android 简单欢迎页面设计
  9. 查看Android ADT Plugin版本的方法
  10. 修改Android Studio编码格式