2015-06-21 56 views
0

我在這裏看到了一些其他線程處理相同的問題,但他們都沒有幫助。Android Studio沒有這樣的表錯誤

我試圖顯示數據庫中的內容,但每當我嘗試運行我的程序,我不斷收到一個錯誤:

06-21 16:25:51.723 2241-2241/net.learn2develop.Databases I/art﹕ Not late-enabling -Xcheck:jni (already on) 
06-21 16:25:51.811 2241-2241/net.learn2develop.Databases D/++++﹕ Here 
06-21 16:25:51.811 2241-2241/net.learn2develop.Databases E/SQLiteLog﹕ (1) no such table: pokemon 
06-21 16:25:51.811 2241-2241/net.learn2develop.Databases D/AndroidRuntime﹕ Shutting down VM 
    --------- beginning of crash 
06-21 16:25:51.812 2241-2241/net.learn2develop.Databases E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: net.learn2develop.Databases, PID: 2241 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learn2develop.Databases/net.learn2develop.Databases.DatabasesActivity}: android.database.sqlite.SQLiteException: no such table: pokemon (code 1): , while compiling: SELECT _id, name, type, 0 FROM pokemon 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: android.database.sqlite.SQLiteException: no such table: pokemon (code 1): , while compiling: SELECT _id, name, type, 0 FROM pokemon 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316) 
      at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163) 
      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034) 
      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202) 
      at net.learn2develop.Databases.DBAdapter.getAllContacts(DBAdapter.java:98) 
      at net.learn2develop.Databases.DatabasesActivity.onCreate(DatabasesActivity.java:48) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5257) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

這裏是我的代碼:數據庫活動

package net.learn2develop.Databases; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ListView; 

public class DatabasesActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     DBAdapter db = new DBAdapter(this); 

     try { 
      String destPath = "/data/data/" + getPackageName() + 
       "/databases"; 
      File f = new File(destPath); 
      if (!f.exists()) {    
       f.mkdirs(); 
       f.createNewFile(); 

       //---copy the db from the assets folder into 
       // the databases folder--- 
       CopyDB(getBaseContext().getAssets().open("pokemon"), 
         new FileOutputStream(destPath + "/PokemonList")); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     //---get all contacts--- 
     db.open(); 
     Log.d("++++", "Here"); 

     Cursor c = db.getAllContacts(); 

     ListView lvItems = (ListView) findViewById(R.id.lvItems); 

     ContactCursorAdapter contactAdapter = new ContactCursorAdapter(this, c); 

     lvItems.setAdapter(contactAdapter); 

     db.close(); 

    } 

    public void CopyDB(InputStream inputStream, 
    OutputStream outputStream) throws IOException { 
     //---copy 1K bytes at a time--- 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = inputStream.read(buffer)) > 0) { 
      outputStream.write(buffer, 0, length); 
     } 
     inputStream.close(); 
     outputStream.close(); 
    } 


} 

聯繫光標適配器

package net.learn2develop.Databases; 

import android.content.Context; 
import android.database.Cursor; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CursorAdapter; 
import android.widget.TextView; 

public class ContactCursorAdapter extends CursorAdapter { 

    public ContactCursorAdapter (Context context, Cursor cursor) { 
     super(context, cursor, 0); 
    } 

    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.lvuser, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView tvPokemon = (TextView) view.findViewById(R.id.tvPokemon); 
     TextView tvType = (TextView) view.findViewById(R.id.tvType); 
     TextView tvGen = (TextView) view.findViewById(R.id.tvGen); 

     String name = cursor.getString(cursor.getColumnIndexOrThrow("pokemon")); 
     String type = cursor.getString(cursor.getColumnIndexOrThrow("type")); 
     int gen = cursor.getInt(cursor.getColumnIndexOrThrow("generation")); 
     tvPokemon.setText(name); 
     tvType.setText(type); 
     tvGen.setInputType(gen); 
    } 



} 

將對DBAdapter

package net.learn2develop.Databases; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

import java.security.Key; 

public class DBAdapter { 
    static final String KEY_ROWID = "_id"; 
    static final String KEY_POKEMON = "name"; 
    static final String KEY_TYPE = "type"; 
    static final int KEY_GEN = 0; 

    static final String TAG = "DBAdapter"; 

    static final String DATABASE_NAME = "PokemonList"; 
    static final String DATABASE_TABLE = "pokemon"; 
    static final int DATABASE_VERSION = 3; 

    static final String DATABASE_CREATE = 
      "CREATE TABLE pokemon (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT," 
      + " type TEXT, gen INTEGER)"; 

    final Context context; 

    DatabaseHelper DBHelper; 
    SQLiteDatabase db; 
    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      try { 
       db.execSQL(DATABASE_CREATE); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS contacts"); 
      onCreate(db); 
     } 
    } 

    //---opens the database--- 
    public DBAdapter open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---closes the database--- 
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //---insert a contact into the database--- 
    public long insertContact(String name, String type, int num) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_POKEMON, name); 
     initialValues.put(KEY_TYPE, type); 
     initialValues.put(String.valueOf(KEY_GEN), num); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //---deletes a particular contact--- 
    public boolean deleteContact(long rowId) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
    } 

    //---retrieves all the contacts--- 
    public Cursor getAllContacts() 
    { 
     return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_POKEMON, 
       KEY_TYPE, String.valueOf(KEY_GEN)}, null, null, null, null, null); 
    } 

    //---retrieves a particular contact--- 
    public Cursor getContact(long rowId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
       KEY_POKEMON, KEY_TYPE, String.valueOf(KEY_GEN)}, KEY_ROWID + "=" + rowId, null, 
       null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    //---updates a contact--- 
    public boolean updateContact(long rowId, String name, int num, String type) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_POKEMON, name); 
     args.put(KEY_TYPE, type); 
     args.put(String.valueOf(KEY_GEN), num); 
     return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
    } 

} 

下面是從DB瀏覽器中創建語句的副本的SQLite

CREATE TABLE "pokemon" (
    `_id` INTEGER PRIMARY KEY AUTOINCREMENT, 
    `name` TEXT, 
    `type` TEXT, 
    `gen` INTEGER 
) 

我在我的項目的資產文件夾中的SQLite數據庫文件。

我不確定如果我錯誤地執行了DATABASE_CREATE語句,我錯誤地設置了實際的數據庫文件,或者我忽略了其他內容。

+0

你能找到你的數據庫中的表? – Prudhvi

+0

我可以在app \ source \ assets中的Android Studio中看到它,並且該應用程序顯示在模擬器中。不過,我不確定在數據庫的模擬器本身的內部,它應該在應用程序內部。 – Monkey

+0

** assets'文件夾**錯位** –

回答

0

的問題是,你沒有使用引號「」

嘗試做這樣的事情

private static final String DATABASE_CREATE = "create table " 
      + TABLE_MOVIE 
      + COLUMN_ID + " integer primary key autoincrement, " 
      + COLUMN_LAST_BUILD_DATE + " varchar(50) not null, " 
      + COLUMN_TITLE + " varchar(100) not null, " 
      + COLUMN_LINK + " varchar(100) not null, " 
      + COLUMN_DESCRIPTION + " varchar(1000) not null, " 
      + COLUMN_PUBDATE + " varchar(50) not null " 
      + ");";