Android Studio没有这样的表错误

时间:2015-06-21 20:27:09

标签: sqlite 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 Browser for SQLite

的create语句的副本
CREATE TABLE "pokemon" (
    `_id`   INTEGER PRIMARY KEY AUTOINCREMENT,
    `name`  TEXT,
    `type`  TEXT,
    `gen`   INTEGER
)

我的项目的assets文件夹中有sqlite数据库文件。

我不确定我是否错误地执行了DATABASE_CREATE语句,我的实际数据库文件设置不正确,或者我忽略了其他内容。

1 个答案:

答案 0 :(得分:0)

问题在于你没有使用引号&#34;&#34;

尝试做这样的事情

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