没有从数据库中获取新创建的表

时间:2011-09-01 05:42:22

标签: android android-layout android-emulator android-widget android-ndk

我的数据库适配器代码如下所示:

package com.quiz.spellingquiz;
import java.io.IOException;

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.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DBAdapter 
{
    //  Database Operation
    public static final String KEY_ROWID = "_id";
    public static final String KEY_ISBN = "isbn";
    public static final String KEY_TITLE = "title";
    public static final String KEY_WORD = "word";
    public static final String KEY_SOUND = "sound";
    public static final String KEY_PUBLISHER = "publisher";    
    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "testing";
    public static String DATABASE_TABLE = null;
    private static final int DATABASE_VERSION = 1;

    public static final String DATABASE_CREATE =
        "create table "+DATABASE_TABLE+" (_id integer primary key, "
        + "isbn text not null," 
        + "title text not null,"
        + "word text not null,"
        + "sound text not null,"
        + "publisher text not null);";


    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;


    public DBAdapter(Context ctx, String table_name) 
    {
        this.context = ctx;
        DATABASE_TABLE=table_name;
        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) 
        {
            db.execSQL(DATABASE_CREATE);

        }

        @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 "+DATABASE_TABLE);
            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 title into the database---
    public long insertTitle(String isbn, String title, String word, String sound, String publisher) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ISBN, isbn);
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_WORD, word);
        initialValues.put(KEY_SOUND, sound);
        initialValues.put(KEY_PUBLISHER, publisher);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

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

    public boolean deleteAllTitle()
    {
        return db.delete(DATABASE_TABLE,null, null)>0;
    }
    public Cursor getAllTitles() 
    {
        Cursor mCursor = db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_ISBN,
                KEY_TITLE,
                KEY_WORD,
                KEY_SOUND,
                KEY_PUBLISHER}, 
                null, 
                null, 
                null,
                null, 
                null,
                null);

        return mCursor;
    }


    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException 
    {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_ISBN, 
                        KEY_TITLE,
                        KEY_WORD,
                        KEY_SOUND,
                        KEY_PUBLISHER
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, ////////////////////////////
                        null);
        if (mCursor != null) 
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    // to fetch TextFile with Sound file
    public Cursor getSound(String str) throws SQLException
    {
        String file = Environment.getExternalStorageDirectory().getAbsolutePath();
        file = file+"/"+str+".3gp";

        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ISBN, 
                    KEY_TITLE,
                    KEY_WORD,
                    KEY_SOUND,
                    KEY_PUBLISHER
                }, 
                KEY_SOUND + "=" +file, 
                null,
                null, 
                null, 
                null,
                null);
if (mCursor != null) 
{
    mCursor.moveToFirst();
}
return mCursor;

    }

    public void deleteAll()
    {
        this.db.delete(DATABASE_TABLE, null, null);
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String isbn, String title,String word,String sound, String publisher) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_ISBN, isbn);
        args.put(KEY_TITLE, title);
        args.put(KEY_WORD, word);
        args.put(KEY_SOUND, sound);
        args.put(KEY_PUBLISHER, publisher);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

现在在另一个活动中,我使用如下表格名称生成DatabaseAdapter对象:

final DBAdapter db = new DBAdapter(this,table_name); // create new database with givan table name

现在,当我从该对象获取数据时,它给出了如下错误:

android.database.sqlite.SQLiteException: no such table: hello: , while compiling: INSERT INTO hello(word, title, sound, publisher, isbn) VALUES(?, ?, ?, ?, ?);

所以我错了?为什么我无法创建表格?我该怎么办?

请参阅这是我调用DBAdapter的另一个活动的代码

  // database object        
    final DBAdapter db = new DBAdapter(this); // create new database with givan table name


    showall.setOnClickListener(new View.OnClickListener() 
    {

        public void onClick(View v) 
        {
            db.open();
            Cursor c = db.getAllTitles();
            while(c.moveToNext())        
            {
                Toast.makeText(EnterWordsActivity.this,
                        "id: " + c.getString(0) + "\n" + 
                        "ISBN: " + c.getString(1) + "\n" +  
                        "TITLE: " + c.getString(2) + "\n" + 
                        "WORD: " + c.getString(3) + "\n" +
                        "SOUND:" + c.getString(4) +"\n"+
                        "PUBLISHER:  " + c.getString(5),Toast.LENGTH_LONG).show();
            }
            db.close();
        }
    });

将新表创建到同一数据库后的错误日志。

09-01 15:05:52.608: ERROR/AndroidRuntime(596): FATAL EXCEPTION: main

09-01 15:05:52.608:ERROR / AndroidRuntime(596):android.database.sqlite.SQLiteException:没有这样的表:world :,同时编译:SELECT _id,isbn,title,word,sound,publisher FROM世界 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteCompiledSql.native_compile(本机方法) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:64) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:80) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:46) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1301) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at com.quiz.spellingquiz.DBAdapter.getAllTitles(DBAdapter.java:119) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at com.quiz.spellingquiz.EnterWordsActivity $ 1.onClick(EnterWordsActivity.java:86) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.view.View.performClick(View.java:2408) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.view.View $ PerformClick.run(View.java:8816) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.os.Handler.handleCallback(Handler.java:587) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.os.Handler.dispatchMessage(Handler.java:92) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.os.Looper.loop(Looper.java:123) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):在android.app.ActivityThread.main(ActivityThread.java:4627) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at java.lang.reflect.Method.invokeNative(Native Method) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at java.lang.reflect.Method.invoke(Method.java:521) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-01 15:05:52.608:ERROR / AndroidRuntime(596):at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:2)

我认为这里的问题是你没有为你的数据库名称声明.db扩展名

 private static final String DATABASE_NAME = "testing";

应该是

 private static final String DATABASE_NAME = "testing.db";

当我尝试你的代码时,这肯定会有用。

还有一个变化就是这个

@Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL("create table "+DATABASE_TABLE+" (_id integer primary key, "
                    + "isbn text not null," 
                    + "title text not null,"
                    + "word text not null,"
                    + "sound text not null,"
                    + "publisher text not null);");
        }

添加以上代码而不是

@Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

然后就像这样调用这个类,

DBAdapter adapter = new DBAdapter(this,"mytable");

答案 1 :(得分:1)

一开始,当final string DATABASE_CREATE将被创建时,string DATABASE_TABLE将为null。因此,每次使用DATABASE_CREATE时,查询都是:

DATABASE_CREATE = "create table (_id integer primary key, "
    + "isbn text not null," 
    + "title text not null,"
    + "word text not null,"
    + "sound text not null,"
    + "publisher text not null);"

此查询(没有表名)无效。

=== update ===

string DATABASE_CREATE中使用模板名称替换变量DATABASE_TABLE

public static final String DATABASE_CREATE =
    "create table #table_name# (_id integer primary key, "
    + "isbn text not null," 
    + "title text not null,"
    + "word text not null,"
    + "sound text not null,"
    + "publisher text not null);";

并替换onCreate中的模板名称:

@Override
public void onCreate(SQLiteDatabase db) 
{
    String query = DATABASE_CREATE.replace("#table_name#", DATABASE_TABLE);
    db.execSQL(query);
}

答案 2 :(得分:1)

使用此代码

 import java.io.IOException;

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.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DBAdapter 
{
//  Database Operation
public static final String KEY_ROWID = "_id";
public static final String KEY_ISBN = "isbn";
public static final String KEY_TITLE = "title";
public static final String KEY_WORD = "word";
public static final String KEY_SOUND = "sound";
public static final String KEY_PUBLISHER = "publisher";    
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "testing";
public static String DATABASE_TABLE = null;
private static final int DATABASE_VERSION = 1;

public static final String DATABASE_CREATE =
    "create table "+DATABASE_TABLE+" (_id integer primary key, "
    + "isbn text not null," 
    + "title text not null,"
    + "word text not null,"
    + "sound text not null,"
    + "publisher text not null);";


private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;


public DBAdapter(Context ctx, String table_name) 
{
    this.context = ctx;
    DATABASE_TABLE=table_name;
    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) 
    {
        db.execSQL(DATABASE_CREATE);

    }

    @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 "+DATABASE_TABLE);
        onCreate(db);
    }
}    

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

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

//used to create the the new table
public void createNewTable(String t_name){
    this.DATABASE_TABLE=t_name;
    db.execSQL(this.DATABASE_CREATE);
}


//---insert a title into the database---
public long insertTitle(String isbn, String title, String word, String sound, String publisher) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_ISBN, isbn);
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_WORD, word);
    initialValues.put(KEY_SOUND, sound);
    initialValues.put(KEY_PUBLISHER, publisher);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

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

public boolean deleteAllTitle()
{
    return db.delete(DATABASE_TABLE,null, null)>0;
}
public Cursor getAllTitles() 
{
    Cursor mCursor = db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID, 
            KEY_ISBN,
            KEY_TITLE,
            KEY_WORD,
            KEY_SOUND,
            KEY_PUBLISHER}, 
            null, 
            null, 
            null,
            null, 
            null,
            null);

    return mCursor;
}


//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException 
{
    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_ISBN, 
                    KEY_TITLE,
                    KEY_WORD,
                    KEY_SOUND,
                    KEY_PUBLISHER
                    }, 
                    KEY_ROWID + "=" + rowId, 
                    null,
                    null, 
                    null, 
                    null, ////////////////////////////
                    null);
    if (mCursor != null) 
    {
        mCursor.moveToFirst();
    }
    return mCursor;
}
// to fetch TextFile with Sound file
public Cursor getSound(String str) throws SQLException
{
    String file = Environment.getExternalStorageDirectory().getAbsolutePath();
    file = file+"/"+str+".3gp";

    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_ISBN, 
                KEY_TITLE,
                KEY_WORD,
                KEY_SOUND,
                KEY_PUBLISHER
            }, 
            KEY_SOUND + "=" +file, 
            null,
            null, 
            null, 
            null,
            null);
    if (mCursor != null) 
  { 
   mCursor.moveToFirst();
  }
 return mCursor;

}

public void deleteAll()
{
    this.db.delete(DATABASE_TABLE, null, null);
}

//---updates a title---
public boolean updateTitle(long rowId, String isbn, String title,String word,String sound, String publisher) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_ISBN, isbn);
    args.put(KEY_TITLE, title);
    args.put(KEY_WORD, word);
    args.put(KEY_SOUND, sound);
    args.put(KEY_PUBLISHER, publisher);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}

}

在您的活动中

db.open();
      db.createNewTable("ur table name");

这解决了你的问题

答案 3 :(得分:0)

如果未显示新的更改设置,那么我认为您应该更新数据库版本。 这将执行新的更改并保存它们