无此类表致命异常:SQLite

f3temu5u  于 2023-04-21  发布在  SQLite
关注(0)|答案(1)|浏览(124)

我是Android的初学者。我正在通过SQLite创建数据库。我创建了阿拉伯语数据库文件,并将其放入名为sample.db的资产文件夹中。现在我试图从资产文件夹中读取数据库,但我收到一个错误,应用程序崩溃。最好的方法是什么?任何帮助都将不胜感激。
这是www.example.com中的代码DbHelper.java

package ...
import ...

public class DbHelper extends SQLiteOpenHelper {
    String dbName;
    Context context;
    String dbPath;

    String tableName = "Dictionary";
    String ArabicCol = "Arabic";

    public DbHelper(MainActivity mcontext, String name, int version) {
        super(mcontext, name, null, version);

        this.context = mcontext;
        this.dbName = name;
        this.dbPath = "/data/data" + "com.example.e_qtishaddictionary3" + "databases";
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(newVersion>oldVersion)
            CopyDatabase();
    }
    public void CheckDb(){
    SQLiteDatabase CheckDb =  null;
            try {
                String filePath = dbPath + dbName;

                CheckDb = SQLiteDatabase.openDatabase(filePath, null, 0);
            }catch (Exception e) {
            }

            if (CheckDb != null) {
                Log.d("CheckDb", "Database already exists");
                CheckDb.close();
            } else {
                CopyDatabase();
            }
        }
        public void CopyDatabase() {
            this.getReadableDatabase();
            try{
                InputStream is = context.getAssets().open(dbName);
                OutputStream os = new FileOutputStream(dbPath + dbName);

                byte[] buffer = new byte[1024];

                int len;
                while((len = is.read(buffer))>0){
                    os.write(buffer,0,len);
                }
                os.flush();
                is.close();
                os.close();
            }catch (Exception e) {e.printStackTrace();}
            Log.d("CopyDb", "Database Copied");
        }
        public void OpenDatabase(){
            String filepath = dbPath + dbName;
            SQLiteDatabase.openDatabase(filepath,null,0);
        }
        public ArrayList<String> getArabicWord(String query){
        ArrayList<String> arabicList = new ArrayList<>();

        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

            Cursor cursor;
            cursor = sqLiteDatabase.query(
                    tableName,
                    new String[]{ArabicCol},
                    ArabicCol + " LIKE ? ",
                    new String[]{query + "%"},
                    null, null,
                    ArabicCol
            );
            int index = cursor.getColumnIndex(ArabicCol);
            while (cursor.moveToNext()){
                arabicList.add(cursor.getString(index));
            }

            sqLiteDatabase.close();
            cursor.close();
            return arabicList;
        }
}

MainActivity.java

package ...
import ...

public class MainActivity extends AppCompatActivity {
    AutoCompleteTextView autoCompleteTextView;
    DbHelper dbHelper;

    ArrayList<String> newList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoCompleteTextView = findViewById(R.id.autotxt);
        dbHelper = new DbHelper(this,"sample.db",1);
        try{
           dbHelper.CheckDb();
           dbHelper.OpenDatabase();
        }catch(Exception e) {
            e.printStackTrace();
        }

        newList = new ArrayList<>();

        autoCompleteTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if(s.length() > 0 ){
                    newList.addAll(dbHelper.getArabicWord(s.toString()));

                    autoCompleteTextView.setAdapter(new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, newList));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }
}

这是我的logcat的一部分

(1) no such table: Dictionary in "SELECT Arabic FROM Dictionary WHERE Arabic LIKE ?  ORDER BY Arabic"
2023-04-20 03:27:40.261 17406-17406 AndroidRuntime          com.example.e_qtishaddictionary3     D  Shutting down VM
2023-04-20 03:27:40.264 17406-17406 AndroidRuntime          com.example.e_qtishaddictionary3     E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.e_qtishaddictionary3, PID: 17406
                                                                                                    android.database.sqlite.SQLiteException: no such table: Dictionary (code 1 SQLITE_ERROR[1]): , while compiling: SELECT Arabic FROM Dictionary WHERE Arabic LIKE ?  ORDER BY Arabic
                                                                                                        at

以及相关线路

at com.example.e_qtishaddictionary3.DbHelper.getArabicWord(DbHelper.java:84)
                                                                                                        at com.example.e_qtishaddictionary3.MainActivity$1.onTextChanged(MainActivity.java:44)

我已经看了我的代码2天了,但是我不知道我哪里错了。你能帮我吗?谢谢。

68bkxrlz

68bkxrlz1#

首先,您需要修复数据库的URL

this.dbPath = "/data/data" + "com.example.e_qtishaddictionary3" + "databases";

像下面一样

this.dbPath = "/data/data/" + "com.example.e_qtishaddictionary3/" + "databases/"

其次,您忘记添加命令来首先创建表本身Dictionary,因此您需要在**void onCreate(SQLiteDatabase sqliteDatabase)**上添加下面的表创建sql命令

sqLiteDatabase.execSQL("CREATE TABLE " + tableName + " ("
            + ArabicCol +" TEXT not null);");

相关问题