安卓資料庫 找不到表

來源:互聯網
上載者:User

標籤:ons   catch   ati   name   generated   length   使用   database   help   

自己建一個表,放在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();
}

}

安卓資料庫 找不到表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.