質問

Hello I am new the the whole android and SQLite database. The problem I'm having is when I try and insert the score to the database it, doesn't even try to. The code continues to run as normal without inserting.

My database table Is literally the primary key id and the score field which is an integer.

Im not entirely sure what code you will need to see to identify my problem but here's my guesstimation. Apologies if I'm wrong I'll try to help in any way I can. Thanks for your time.

DBAdaptor class.

//Primary Key for the score
    public static final String KEY_ROWID = "id";
    //Field for user score
    public static final String KEY_SCORE = "score";

    //Database name
    private static final String DATABASE_NAME = "SportsQuiz";
    //Database table name
    private static final String DATABASE_TABLE = "score";
    //Database version
    private static final int DATABASE_VERSION = 1;

    private dbHelper dbHelper;
    private final Context context;
    private SQLiteDatabase sqlDB;

    private static class dbHelper extends SQLiteOpenHelper
    {
        //Constructor for dbHelper
        public dbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

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

        @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 Adapter(Context c)
    {
        context = c;
    }

    public Adapter open() throws SQLException
    {
        dbHelper = new dbHelper(context);
        sqlDB = dbHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        dbHelper.close();
    }

    public long createEntry(int score) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_SCORE, score);
        return sqlDB.insert(DATABASE_TABLE, null, cv);
    }

In my game class I wish to display a dialog box if the insert was successful. Score is an int variable.

boolean working = true;
                    try{
                        Adapter entry = new Adapter(Game.this);
                        entry.open();
                        entry.createEntry(score);

                        entry.close();
                    }catch (Exception e)
                    {
                        working = false;
                    }
                    finally
                    {
                        if (working == true)
                        {
                            Dialog d = new Dialog(Game.this);
                            d.setTitle("Woo!");
                            TextView tv = new TextView(Game.this);
                            tv.setText("Woo");
                            d.setContentView(tv);
                            d.show();
                        }
                    }
役に立ちましたか?

解決

There's a space missing between KEY_SCORE and its type in your database creation SQL. Also the ; at the end is misplaced. Change to:

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " + 
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_SCORE + " INTEGER NOT NULL);");

After fixing it, uninstall your app so the database gets recreated. Your onUpgrade() has also syntax problems so it's no good just upgrading the database version.

It's also a good habit to log exceptions to learn what's wrong with the code. Computers are much better than humans in detecting syntax problems.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top