Search code examples
androidsqlitecursor

Cursor or ContentValues can't read my SQLite data? :D


I have a problem while following thenewboston Android tut's

So the first problem is i'm sure that i have followed what he has done exactly, but this time my app is not working.

Sorry if my post will be too long, YOU CAN EDIT IT if you can make my question clear

So this is a SQLiteExample Class that i extended Activity :

public class SQLiteExample extends Activity implements OnClickListener{
Button sqlUpdate, sqlView;
EditText sqlName, sqlHotness;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlliteexample);

    sqlUpdate = (Button) findViewById(R.id.bSqlUpdate);
    sqlView = (Button) findViewById(R.id.bSqlopenView);
    sqlName = (EditText) findViewById(R.id.etSqlName);
    sqlHotness = (EditText) findViewById(R.id.etSqlHot);

    sqlUpdate.setOnClickListener(this);
    sqlView.setOnClickListener(this);
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.bSqlUpdate :

        boolean didItWork = true;

        try {
            String name = sqlName.getText().toString();
            String hotness = sqlHotness.getText().toString();

            HotOrNot entry = new HotOrNot(SQLiteExample.this);
            entry.open();
            entry.createEntry(name, hotness);
            entry.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Dang It!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{
            if(didItWork){
                Dialog d = new Dialog(this);
                d.setTitle("Heck Yea!");
                TextView tv = new TextView(this);
                tv.setText("Success");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    case R.id.bSqlopenView :
        Intent i = new Intent("com.thenewboston.tama.SQLView");
        startActivity(i);
        break;
    }
}

I just setOnClickListener() to those 2 button's here.

This is my HotOrNot Class which hold the SQLite stuff's :

public class HotOrNot {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";

private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {    

    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_NAME + " TEXT NOT NULL, " + 
                KEY_HOTNESS + "TEXT 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 HotOrNot(Context c){
    ourContext = c;
}
//?????????????????????
public HotOrNot open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

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

public long createEntry(String name, String hotness) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iHotness = c.getColumnIndex(KEY_HOTNESS);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result += c.getString(iRow) + " " + c.getString(iName) + " " +c.getString(iHotness) + "\n";
    }

    return result;
}

And this is my SQLView extended Activity (for the 2nd Button) :

public class SQLView extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);

    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    HotOrNot info = new HotOrNot(this);
    info.open();
    String data = info.getData();
    info.close();

    tv.setText(data);
}

And this is the error that i got when i click the R.id.bSqlopenView button in my SQLiteExample Class:

03-19 14:47:54.504: I/Database(335): sqlite returned: error code = 1, msg = table peopleTable has no column named persons_hotness

And a lot of error's again after that first one

EDITED : This my FULL VERSION Error List :D

    03-19 14:47:28.194: D/dalvikvm(335): GC_EXTERNAL_ALLOC freed 50K, 53% free 2554K/5379K, external 1625K/2137K, paused 107ms
03-19 14:47:49.634: W/KeyCharacterMap(335): No keyboard for id 0
03-19 14:47:49.634: W/KeyCharacterMap(335): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-19 14:47:54.504: I/Database(335): sqlite returned: error code = 1, msg = table peopleTable has no column named persons_hotness
03-19 14:47:54.515: E/Database(335): Error inserting persons_hotness= persons_name=
03-19 14:47:54.515: E/Database(335): android.database.sqlite.SQLiteException: table peopleTable has no column named persons_hotness: , while compiling: INSERT INTO peopleTable(persons_hotness, persons_name) VALUES(?, ?);
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
03-19 14:47:54.515: E/Database(335):    at com.thenewboston.tama.HotOrNot.createEntry(HotOrNot.java:70)
03-19 14:47:54.515: E/Database(335):    at com.thenewboston.tama.SQLiteExample.onClick(SQLiteExample.java:46)
03-19 14:47:54.515: E/Database(335):    at android.view.View.performClick(View.java:2485)
03-19 14:47:54.515: E/Database(335):    at android.view.View$PerformClick.run(View.java:9080)
03-19 14:47:54.515: E/Database(335):    at android.os.Handler.handleCallback(Handler.java:587)
03-19 14:47:54.515: E/Database(335):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 14:47:54.515: E/Database(335):    at android.os.Looper.loop(Looper.java:123)
03-19 14:47:54.515: E/Database(335):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 14:47:54.515: E/Database(335):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 14:47:54.515: E/Database(335):    at java.lang.reflect.Method.invoke(Method.java:507)
03-19 14:47:54.515: E/Database(335):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 14:47:54.515: E/Database(335):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 14:47:54.515: E/Database(335):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • Put a space after KEY_HOTNESS in the String where you create the database, so instead of:

    //...
    KEY_HOTNESS + "TEXT NOT NULL); "
    

    write:

       //...
       KEY_HOTNESS + " TEXT NOT NULL); "