背景: 我是Android新手,我在资产文件夹中有一个数据库,用户应该能够从操作栏中搜索该数据库。 我不是从代码本身创建该数据库,而是在第一次启动应用程序时,创建一个空数据库,并将我的数据库复制到该空数据库。 现在,当我尝试搜索由于以下错误导致的应用程序崩溃时。 我已复制/编辑了此link的代码。
已尝试解决方案
错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.amgad.hayy / com.amgad.hayy.SearchableActivity}: android.database.sqlite.SQLiteException:没有这样的表:places(代码 1):,同时编译:SELECT类型,名称FROM放置WHERE _id =?
错误代码:1(SQLITE_ERROR)引起:SQL(查询)错误或丢失 数据库。 (没有这样的表:places(代码1):,同时编译:SELECT type,name FROM places WHERE _id =?) #
完成LogCat
11-20 10:52:35.231 28023-28023 / com.amgad.hayy E / AndroidRuntime:致命例外:主要 过程:com.amgad.hayy,PID:28023 java.lang.RuntimeException:无法启动活动ComponentInfo {com.amgad.hayy / com.amgad.hayy.SearchableActivity}:android.database.sqlite.SQLiteException:没有这样的表:places(代码1):,同时编译:SELECT type,name FROM places WHERE _id =? ################################################## ############### 错误代码:1(SQLITE_ERROR) 引起:SQL(查询)错误或缺少数据库。 (没有这样的表:places(代码1):,同时编译:SELECT类型,名称FROM放置WHERE _id =?) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:229) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1821) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:7331) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:android.database.sqlite.SQLiteException:没有这样的表: places(代码1):,同时编译:SELECT类型,名称FROM places WHERE _id =? ################################################## ############### 错误代码:1(SQLITE_ERROR) 引起:SQL(查询)错误或缺少数据库。 (没有这样的表:places(代码1):,同时编译:SELECT类型,名称FROM 地点WHERE _id =?) ################################################## ############### 在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 方法) 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1000) 在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:565) 在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:59) 在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1476) 在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1323) 在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1194) 在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1362) 在com.amgad.hayy.data.HayyDbHelper.getAllEmployeesDescription(HayyDbHelper.java:228) 在com.amgad.hayy.SearchableActivity.showPlaceName(SearchableActivity.java:52) 在com.amgad.hayy.SearchableActivity.onCreate(SearchableActivity.java:44) 在android.app.Activity.performCreate(Activity.java:6904) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) ... 9更多
代码
公共类HayyDbHelper扩展了SQLiteOpenHelper
private static String DB_PATH = "/data/data/com.amgad.hayy/databases/"; static final String DB_NAME = "hayy.db"; private SQLiteDatabase myDataBase; private final Context myContext; private static final int DB_VERSION = 1; public static final String TABLE_NAME = "places"; public static final String COLUMN_PLACE_ID = "_id"; public static final String COLUMN_MONUMENT_NUMBER = "monument_number"; public static final String COLUMN_TYPE = "type"; public static final String COLUMN_NAME = "name"; public HayyDbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); this.myContext = context; } public void createDb() throws IOException{ boolean dbExists = checkDb(); //SQLiteDatabase db_Read = null; if(dbExists){ //do nothing, db already exists }else{ //By calling this method an empty database will be created into the default system path //of the application so we are gonna be able to overwrite that database with our database. //db_Read = this.getReadableDatabase(); //db_Read.close(); this.getReadableDatabase(); try{ // this.close(); copyDb(); }catch(IOException e){ throw new Error("Error copying database"); } } //this.close(); } private boolean checkDb(){ File databasePath = myContext.getDatabasePath(DB_NAME); return databasePath.exists(); } private void copyDb() 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 openDb() 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){ db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); } public ArrayList<String> getAllName() { SQLiteDatabase db = this.getReadableDatabase(); ArrayList<String> pNames = new ArrayList<String>(); Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); do { String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME)); pNames.add(name); } while (cursor.moveToNext()); } cursor.close(); db.close(); return pNames; } public Cursor getAllEmployeesDescription(int id) { SQLiteDatabase db = this.getReadableDatabase(); String[] selections = {String.valueOf(id)}; String columns[] = {COLUMN_TYPE, COLUMN_NAME}; Cursor cursor = db.query(TABLE_NAME, columns, COLUMN_PLACE_ID + "=?", selections, null, null, null); //db.close(); return cursor; }
主要方法
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDatabaseHelper = new HayyDbHelper(MainActivity.this); try { myDatabaseHelper.createDb(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { myDatabaseHelper.openDb(); }catch(SQLException sqle){ throw sqle; } }
答案 0 :(得分:0)
我不知道你做了什么。如果您正在使用多个表,并且在使用SQLiteHandler运行应用程序之后,您已经创建了一些表,之后您尝试再创建一个表,问题是SQLiteHandler中的onCreate只被调用一次。尝试从测试设备(模拟器/手机)卸载应用程序并重新安装。