代码:

package com.example.ch_2013_4_5sqlite;import android.annotation.SuppressLint;import android.content.Context;import android.database.sqlite.SQLiteDatabase;@SuppressLint("SdCardPath")public class sqliteDB {//SQLiteDatabase sqLiteDatabase;Context context;//public sqliteDB(Context context) {//this.context = context;}/** * 新建或打开数据库 */public void OnCreateOrOpenDatabase() {sqLiteDatabase = context.openOrCreateDatabase(context.getFilesDir().getAbsolutePath().replace("files", "databases")+ "/" + "mysqlite.db", 0, null);}/** * 建表 */public void createTable() {sqLiteDatabase.execSQL("create table TblUser(id integer primary key autoincrement,name text)");}/** * 插入数据 */public void insertUser() {sqLiteDatabase.execSQL("insert into TblUser(name) values('u1')");}}
打开数据库的方法中 , 如果用SqliteDataBaseSQLiteDatabase 会报打不开数据库的错误。

使用 context.openOrCreateDatabase 会解决这个问题 ,context 是调用上下文( Activity)



更多相关文章

  1. android常用
  2. WiFi的使用Demo
  3. android 打开wifi
  4. Android(安卓)打开和关闭软键盘
  5. Android(安卓)常用系统功能(打开相册,打开系统设置等等)
  6. Android(安卓)监听手机GPS打开状态实现代码
  7. Android(安卓)关于 如何使用外界导入的数据库文件
  8. Android在程序中打开网页
  9. Android连接网络问题

随机推荐

  1. eclipse使用appcompat_v7库无法找到andro
  2. 两个星期的Android开发
  3. AndroidStudio导入新项目一直卡在Buildin
  4. Android术语小全,推荐一下(改日在翻译).
  5. Android开发学习 之 一、开发环境的搭建
  6. Android软件开发之获取通讯录联系人信息
  7. Android(安卓)调用系统Camera
  8. Android开发EditText属性
  9. Android教程之android数据库编程
  10. Android中不常见的监听: 鼠标划过/双击/右