java - SQLite 复制数据库到 Android "no such table"

标签 java android sql sqlite

我刚开始使用 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();
        }
    }
}

更新:

i tested the application an emulator that already has the database in it and the application worked just fine. so my problem is only in moving/copying the database to the new device!

最佳答案

检查查询的语法是否正确:

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+"'";

希望对您有所帮助。

关于java - SQLite 复制数据库到 Android "no such table",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962194/

相关文章:

java - Spring 声明式事务不起作用(事务未提交)

java - pageradapter 中的 getResources 数组 xml

sql - 硬编码函数参数产生 5 倍的加速

sql - 参数化 SQL 查询中变量的影响

java - 从字符串中删除某些字符

java - 用于计算句子中单词的正则表达式

java - Java中的回滚注解

java - 使用图像和环绕图像的文本编辑 TextView

android - android中的图片模糊

java - 我可以使用 Selenium 打开应用程序吗?