我为我的项目创建了自己的数据库,但是我在数据/数据文件夹中将其推送出来时遇到了麻烦
我首先在我的root手机上测试它,然后我成功推送了我创建的数据库。
现在我面临着在不同手机上测试它的问题。我收到错误
android.database.sqlite.SQLiteException: no such table: tblFirstAid: , while compiling: SELECT faName FROM tblFirstAid
我将数据库放在assets文件夹中,并且在我的root电话上运行正常。
我将添加什么代码来从资产文件夹中复制文件并将其推送到/ data / data目录中
我已经尝试了一个reigndesign,我收到了这个错误。
这是我的DB Helper Class
package com.dr.droid.lee;
import java.io.IOException;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.http.SslCertificate.DName;
import android.util.Log;
public class DbHelper {
public static final String Row_id = "_id";
public static final String Row_Name = "faName";
public static final String Row_Desc = "faInfo";
private static final String db_Name = "dbDrDroid";
private static final String db_Table = "tblFirstAid";
private static final String db_HTable = "tblHospitals";
private static final String Row_HosName = "HospitalName";
private static final String Row_HosAdd = "Address";
private static final String Row_HosReg = "Region";
private static final String Row_HosCity = "City";
private static final String Row_HosContact = "Contact";
private static final String Row_dName = "dName";
private static final String Row_dTable = "tblDisease";
private static final int db_Version = 1;
private dbhelp ourhelper;
private static Context ourcontext;
private SQLiteDatabase ourDB;
private static class dbhelp extends SQLiteOpenHelper{
public dbhelp(Context context) {
super(context, db_Name, null, db_Version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS " + db_Table + " (" + Row_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Row_Name + " TEXT NOT NULL, "
+ Row_Desc + " TEXT NOT NULL)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + db_Table);
onCreate(db);
}
}
public DbHelper (Context c){
ourcontext= c;
}
public DbHelper open(){
ourhelper = new dbhelp(ourcontext);
ourDB = ourhelper.getWritableDatabase();
return this;
}
public void close(){
ourhelper.close();
}
public ArrayList<String> getFAData() {
// TODO Auto-generated method stub
ArrayList<String> comments = new ArrayList<String>();
String [] columns = new String[]{Row_Name};
Cursor c = ourDB.query(db_Table, columns, null, null, null, null, null);
int iRow = c.getColumnIndex(Row_Name);
c.moveToFirst();
while (!c.isAfterLast()) {
comments.add(c.getString(iRow));
c.moveToNext();
}
c.close();
return comments;
}
public ArrayList<String> getHData(){
ArrayList<String> res = new ArrayList<String>();
String [] columns = new String []{Row_HosName};
Cursor h = ourDB.query(db_HTable, columns, null, null, null, null, Row_HosName);
int HRow = h.getColumnIndex(Row_HosName);
h.moveToFirst();
while (!h.isAfterLast()) {
res.add(h.getString(HRow));
h.moveToNext();
}
h.close();
return res;
}
public ArrayList<String> getDData(){
ArrayList<String> res = new ArrayList<String>();
String [] columns = new String []{Row_dName};
Cursor d = ourDB.query(Row_dTable, columns, null, null, null, null, null);
int HRow = d.getColumnIndex(Row_dName);
d.moveToFirst();
while (!d.isAfterLast()) {
res.add(d.getString(HRow));
d.moveToNext();
}
d.close();
return res;
}
}
这是我在reigndesign中使用的课程
package com.dr.droid.lee;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.dr.droid.lee/databases/";
private static String DB_NAME = "dbDrDroid";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
this.close();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
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;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
如果我不在手机中推送数据库,我仍然会遇到错误。
答案 0 :(得分:0)
我认为您的DB_NAME与您的assests文件夹中存在的数据库不同。
如果您正在关注&#34; reigndesign&#34;,请检查以下一行:
private static String DB_NAME ="myDBName";
此处的DB_NAME是您的数据库的名称。假设您在assets文件夹中有数据库的副本。您的DB_NAME必须与资产文件夹中的相同。
OR
关注this。
答案 1 :(得分:0)
当你给它一个不存在的文件名时,SQLite会很乐意创建一个新的空数据库。
确保用于复制和打开数据库的文件名相同。