[英]SQLite copy database to Android “no such table”
我是使用SQLite的新手,我希望有人能幫助我。
我認為我的問題僅在於SQL類,因為我編輯了該類並以某種方式破壞了它。
我收到此異常,但無法找到表:
04-09 11:24:19.172: E/AndroidRuntime(2026): FATAL EXCEPTION: main
04-09 11:24:19.172: E/AndroidRuntime(2026): Process: saudi.ussd.inc, PID: 2026
04-09 11:24:19.172: E/AndroidRuntime(2026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hmkcode.android/saudi.ussd.inc.MainActivity}: android.database.sqlite.SQLiteException: no such table: Services (code 1): , while compiling: SELECT name FROM Services WHERE sub_name is null
這是我的SQLreader類:
package saudi.ussd.inc;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SQLreader extends SQLiteOpenHelper {
private static String DB_PATH = "data/data/saudi.ussd.inc/databases/" ;
private static final String DB_NAME = "USSDts.sqlite";
private final Context myContext;
public SQLreader(Context context) {
super(context,DB_NAME, null, 1);
myContext = context;
// DB_PATH = context.getFilesDir().getPath()+ "/databases/";
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
this.createDB();
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
//this.createDB();
}
public Service getService(int Id){
SQLiteDatabase db = this.getReadableDatabase();
Service Ser = new Service();
String selectQuery = "select name , start_code , end_code from Services where id = " + Id;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
Ser.setName(c.getString(c.getColumnIndex("name")));
Ser.setStartCode(c.getString(c.getColumnIndex("start_code")));
Ser.setEndCode(c.getString(c.getColumnIndex("end_code")));
return Ser;
}
public ArrayList<String> getAllServicesArray(){
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> AllServices = new ArrayList<String>();
String selectQuery = "SELECT name FROM Services WHERE sub_name is null";
String valueBeforeEdit ;
int indexOfChar;
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
valueBeforeEdit = c.getString(c.getColumnIndex("name"));
indexOfChar = valueBeforeEdit.indexOf("-") != -1 ? valueBeforeEdit.indexOf("-") : valueBeforeEdit.length();
AllServices.add(valueBeforeEdit.substring(0, indexOfChar));
} while (c.moveToNext());
}
return AllServices;
}
public ArrayList<String> getCompaniesName(){
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> CompaniesName = new ArrayList<String>();
String selectQuery = "select name from Companies";
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
CompaniesName.add(c.getString(c.getColumnIndex("name")));
} while (c.moveToNext());
}
return CompaniesName;
}
// closing database
public void closeDB() {
SQLiteDatabase db = this.getReadableDatabase();
if (db != null && db.isOpen())
db.close();
}
private void createDB(){
boolean dbExist = dbExists();
if(!dbExist){
copyDataBase();
}
else if(dbExist){
copyDataBase();
}
}
private boolean dbExists(){
SQLiteDatabase db = null;
try{
String dbPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
}
catch(SQLiteException e){
Log.e("SQL Helper", "database not found");
}
if(db != null){
db.close();
}
return db != null ? true : false;
}
private void copyDataBase(){
Log.i("Database",
"New database is being copied to device!");
byte[] buffer = new byte[1024];
OutputStream myOutput = null;
int length;
// Open your local db as the input stream
InputStream myInput = null;
try
{
myInput =myContext.getAssets().open(DB_NAME);
// transfer bytes from the inputfile to the
// outputfile
myOutput =new FileOutputStream(DB_PATH + DB_NAME);
while((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
myOutput.close();
myOutput.flush();
myInput.close();
Log.i("Database",
"New database has been copied to device!");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
更新 :
我在一個已經有數據庫的模擬器上測試了該應用程序,並且該應用程序運行良好。 所以我的問題僅在於將數據庫移動/復制到新設備!
檢查查詢的正確語法:
String selectQuery = "select name , start_code , end_code from Services where id = " + Id;
請嘗試以下方法:
String selectQuery = "select name , start_code , end_code from Services where id = '"+Id+"'";
希望能幫助到你。
轉到應用程序管理器並清除此應用程序的緩存和數據,然后重試,我認為您已對代碼進行了更改以更新字段/表,但由於仍使用舊的DB結構,因此未在電話上實現,請清除緩存和數據並重新啟動應用程序,它將重新創建數據庫或處理SQLiteOpenHelper類中的升級事件
我相信問題在於您如何/在何處復制數據庫。 在代碼中,您將數據庫文件定義為:
private static String DB_PATH = "data/data/com.hmkcode.android/databases/";
private static final String DB_NAME = "USSDts.sqlite";
然后使用以下代碼創建數據庫文件:
myOutput = new FileOutputStream(DB_PATH + DB_NAME);
當然,這不會在正確的位置創建數據庫(如果有的話)。 相反,您應該像這樣創建數據庫:
File dir = new File(context.getApplicationInfo().dataDir + "/databases");
dir.mkdirs();
File f = new File(dir, DB_NAME);
OutputStream os = new FileOutputStream(f);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.