没有这样的表(代码1)

时间:2014-04-13 18:23:10

标签: java android database sqlite

我收到一条错误消息:

android.database.sqlite.SQLiteException:没有这样的表:DATABASE_TABLE(代码1):,同时编译:SELECT KEY_HOURS来自DATABASE_TABLE,其中KEY_ROWID = 0

当另一个活动调用 upDateUser(String money)

时会发生此问题

我已尝试卸载并重新安装应用程序,就像在其他类似问题上提到的那样,但该解决方案对我自己不起作用。

我的数据库类代码:

package com.example.parkangel;

public class UDbHelper extends SQLiteOpenHelper
{
    private static UDbHelper instance;
    private SQLiteDatabase ourDatabase;

    private static final String DATABASE_NAME = "UserData.db";
    private static final String DATABASE_TABLE = "UserTable";
    private static final int DATABASE_VERSION = 1;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_PFNAME = "payeeFname";
    public static final String KEY_PSNAME = "payeeSname";
    public static final String KEY_CARD = "card";
    public static final String KEY_CREDITS = "credits";
    public static final String KEY_ADDMONEY = "addmoney";

    public UDbHelper(Context context) 
    {   
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static UDbHelper getInstance(Context context)
    {
        if (instance == null)
        {
            instance = new UDbHelper(context);  
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "   
                                    + KEY_PFNAME + " TEXT NOT NULL, " + KEY_PSNAME + "  
                                    TEXT NOT NULL, " + KEY_CARD + " INTEGER NOT NULL, "  
                                    + KEY_CREDITS + " INTEGER NOT NULL, " + 
                                    KEY_ADDMONEY + " INTEGER NOT NULL);");  

        ContentValues values = new ContentValues();
        values.put(KEY_PFNAME, "Tutku"); 
        values.put(KEY_PSNAME, "Erbil");
        values.put(KEY_CARD, 12345678); 
        values.put(KEY_CREDITS, 5);
        values.put(KEY_ADDMONEY, 1);
        db.insert(DATABASE_TABLE, null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

    public synchronized UDbHelper open() throws SQLException
    {
        System.out.println ("running open");
        if(ourDatabase == null || !ourDatabase.isOpen())
        ourDatabase = getWritableDatabase();
        return this;
    }   

    public String getData() 
    {
        // TODO Auto-generated method stub
        String[] columns = new String[] {KEY_ROWID, KEY_PFNAME, KEY_PSNAME,
                        KEY_CARD, KEY_CREDITS};
        Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, null, null, 
                                                   null, null, null);
        String result = " ";

        int iRow = c.getColumnIndexOrThrow(KEY_ROWID);
        int iPFname = c.getColumnIndexOrThrow(KEY_PFNAME);
        int iPSname = c.getColumnIndexOrThrow(KEY_PSNAME);
        int iCard = c.getColumnIndexOrThrow(KEY_CARD);
        int iCredits = c.getColumnIndexOrThrow(KEY_CREDITS);

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow) + " " + 
                            c.getString(iPFname) + " " + c.getString(iPSname) + " " + 
                            c.getString(iCard) + " " + c.getString(iCredits) + "\n";
        }
        return result;
    }

    public List<Integer> getMoneytoadd()
    {  
        List<Integer> list = new ArrayList<Integer>();  

        // Select All Query  
        String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;  

        SQLiteDatabase db = this.getReadableDatabase();  
        Cursor cursor = db.rawQuery(selectQuery,    
                                    null);//selectQuery,selectedArguments  

        // looping through all rows and adding to list  
        if (cursor.moveToFirst()) {  
            do {  
                list.add(cursor.getInt(5));//adding 2nd column data  
            } while (cursor.moveToNext());  
    }  
        // return hours  
        return list;  
} 

          /* public int getcurrentmoney() 
    {
        // TODO Auto-generated method stub
        String[] columns = new String[] {KEY_CREDITS};
        Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, null, null, 
                                                  null, null, null);
        int result = 0;

        int iCredits = c.getColumnIndexOrThrow(KEY_CREDITS);

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = c.getInt(iCredits);
        }
        return result;
    }*/

    public void upDateUser(String money) 
    {
        String selectQuery = "SELECT KEY_CREDITS from DATABASE_TABLE where 
                                  KEY_ROWID = " + 0;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
        int oldMoney = 0;
        if (c.moveToFirst()) 
        {         
            oldMoney = Integer.parseInt(c.getString(4));
        }
        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(KEY_CREDITS, oldMoney + money);
        String filter = "KEY_ROWID" + "=" + 0;
        db.update(DATABASE_TABLE, cvUpdate, filter, null);
    }

2 个答案:

答案 0 :(得分:2)

在此处,您应该从引用的字符串文字中提取KEY_CREDITSDATABASE_TABLEKEY_ROWID,以便使用这些常量变量的值:

String selectQuery = "SELECT KEY_CREDITS from DATABASE_TABLE where 
                              KEY_ROWID = " + 0;

更改为

String selectQuery = "SELECT " + KEY_CREDITS + " from " + DATABASE_TABLE + " where " +
                              KEY_ROWID + " = " + 0;

答案 1 :(得分:1)

尝试使用:

String selectQuery = "SELECT " + KEY_CREDITS + " from " + DATABASE_TABLE+ " where " +  
                                  KEY_ROWID + " = 0";