1. 程式人生 > >安卓數據庫 找不到表

安卓數據庫 找不到表

安卓數據庫 let 系統默認 ado open actor sse filename 創建失敗

自己建一個表,放在assets目錄下

package mine;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MyDatebaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK ="create table user ("
+"id integer primary key autoincrement,"
+"username text not null,"
+"password text not null,"
+"sex text not null,"
+"number text not null);";

private Context mContext;

//用戶數據庫文件的版本
private static final int DB_VERSION = 1;
//數據庫文件目標存放路徑為系統默認位置,cn.arthur.examples 是你的包名
private static String DB_PATH="/data/data/com.example.pinan/databases/";
private String dbpath;

//下面兩個靜態變量分別是目標文件的名稱和在assets文件夾下的文件名
private static String DB_NAME = "huawei.db";
private static String ASSETS_NAME = "huawei.db";


public MyDatebaseHelper(Context context, String name,CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
mContext = context;
try {
createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

//db.execSQL(CREATE_BOOK);
//db.execSQL("insert into userDate(nametext,password) values(‘14‘,‘14‘)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// db.execSQL("drop table if exists user");
// onCreate(db);
}

public void createDataBase() throws IOException {
dbpath = mContext.getFilesDir().getPath().split("files")[0] + "databases/";
boolean dbExist = checkDataBase();
if(dbExist)
{
//數據庫已存在,不做任何操作

}
else
{
//創建數據庫
try {
File dir = new File(dbpath);
if(!dir.exists()){
dir.mkdirs();
}
File dbf = new File(dbpath + DB_NAME);
if(dbf.exists()){
dbf.delete();
}
SQLiteDatabase.openOrCreateDatabase(dbf, null);
// 復制asseets中的數據庫文件到DB_PATH下
copyDataBase();
} catch (IOException e) {
throw new Error("數據庫創建失敗");
}
}
}

//檢查數據庫是否有效
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
String myPath = dbpath + DB_NAME;
try{
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does‘t exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}

/**
* 復制assets文件中的數據庫到指定路徑
* 使用輸入輸出流進行復制
**/
private void copyDataBase() throws IOException{

InputStream myInput = mContext.getAssets().open(ASSETS_NAME);
String outFileName = dbpath + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}

}

安卓數據庫 找不到表