开发者

Not getting new created table from the database

开发者 https://www.devze.com 2023-04-01 21:00 出处:网络
My Database Adapter code is as like below: package com.quiz.spellingquiz; import java.io.IOException; import android.content.ContentValues;

My Database Adapter code is as like below:

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 =开发者_如何学Python 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;
    }
}

Now in another activity i make the DatabaseAdapter object with the table name like this:

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

Now while i fetch the data from that object, it gives me error like :

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

So where i am wrong? Why i am not able to create the table? What should i have to do ?

See This is My code of another activity from where i m calling the 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();
        }
    });

Error log after creating new table in to same database.

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: no such table: world: , while compiling: SELECT _id, isbn, title, word, sound, publisher FROM world 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at 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): at android.view.View.performClick(View.java:2408) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.view.View$PerformClick.run(View.java:8816) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.os.Handler.handleCallback(Handler.java:587) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.os.Handler.dispatchMessage(Handler.java:92) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at android.os.Looper.loop(Looper.java:123) 09-01 15:05:52.608: ERROR/AndroidRuntime(596): at 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)


Well I think the problem here is you are not declaring the .db extension for your database name

 private static final String DATABASE_NAME = "testing";

It should be

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

This will surely work,as I tried your code.

And one more change is this

@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);");
        }

Add this above code instead of

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

Then just call this class like this,

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


use this code

 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;
}

}

in your activity

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

this solve ur problem


At the start, when the final string DATABASE_CREATE will be created the string DATABASE_TABLE will be null. So everytime DATABASE_CREATE is used, the query is:

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

This query (without table name) is not valid.

=== update ===

In the string DATABASE_CREATE replace the variable DATABASE_TABLE with a template name:

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

And replace the template name in the onCreate:

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


If your new changed settings are not shown then i think you should update the Database version. This will execute the new changes and save them

0

精彩评论

暂无评论...
验证码 换一张
取 消