-3

I have created a table named User in SQLite which consists of 3 column, Name, Password and Image.

MyDatabaseHelper

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="mm.db";
    public static final String TABLE_USER="User";
    public static final String KEY_NAME="Name";
    public static final String KEY_PASSWORD="Password";
    public static final String KEY_IMAGE="Image";
    public static final String ID="id";

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_USER + " ( " + ID + " INTEGER PRIMARY KEY ,Name TEXT,Password TEXT,Image BLOB )");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
        Log.w(MyDatabaseHelper.class.getName(), "Upgrading database from version" + oldVersion + "to" + newVersion + ",which will destroy all old data");
        db.execSQL("Drop TABLE IF EXISTS " + TABLE_USER);
        onCreate(db);
    }

    public MyDatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME,null,1);
    }
}

When I tried to retrieve image from SQLite and placed into ImageView, I get error, but when I insert data, it works fine.

MainActivity

public class MainActivity extends AppCompatActivity {
    EditText name;
    private MyDatabaseHelper mdb=null;
    private SQLiteDatabase database= null;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mdb = new MyDatabaseHelper(this);
        database = mdb.getWritableDatabase();
        Button createAccount = (Button) findViewById(R.id.btnLinkToRegisterScreen);
        Bitmap picture = BitmapFactory.decodeResource(getResources(), R.mipmap.profile);
        imageView = (ImageView) findViewById(R.id.imgProfilePicture);
        imageView.setImageBitmap(getRoundedBitmap(picture));
        name = (EditText) findViewById(R.id.name);

        createAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String personName = name.getText().toString();
                String selectQuery = " SELECT " + MyDatabaseHelper.KEY_IMAGE + " FROM " + MyDatabaseHelper.TABLE_USER + " WHERE " + MyDatabaseHelper.KEY_NAME + " = " +personName;
                Cursor cursor = database.rawQuery(selectQuery, null);
                if (cursor.moveToFirst()) {
                    byte[] temp_image = cursor.getBlob(1);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(temp_image, 0, temp_image.length);
                    imageView.setImageBitmap(bitmap);
                }
            }
        });
    }
}

Error

09-12 17:33:09.083  14231-14231/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such column: alex (code 1): , while compiling: SELECT Image FROM User WHERE Name = alex
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:888)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:499)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
            at com.example.seng.healthyapp.MainActivity$1.onClick(MainActivity.java:76)
            at android.view.View.performClick(View.java:4230)

The alex is actually a userName I typed, but it said no such column.

12
  • Uninstall app and run again. It will work :) Sep 12, 2016 at 9:39
  • 1
    You need to put alex in ' ' Sep 12, 2016 at 9:40
  • 1
    SQL fundamentals!! WHERE Name = 'alex'. Or better: use bound parameters. Sep 12, 2016 at 9:40
  • @AnandSavjani No, it won't. Sep 12, 2016 at 9:41
  • 1
    John, SO is not a Google replacement. And it's a good training for you (for everyone, actually) to search and find tutorials about new things. SO is the last resort when you searched for hours and could land to nothing. Then, and only then, SO is your gold mine. But be sure to search for already answered questions, first. Sep 12, 2016 at 10:58

1 Answer 1

0

Enclose your string in single quotes, like

SELECT Image FROM User WHERE Name = 'alex'
3
  • what if I want to have a where clause ?
    – John
    Sep 12, 2016 at 9:43
  • String selectQuery = " SELECT " + MyDatabaseHelper.KEY_IMAGE + " FROM " + MyDatabaseHelper.TABLE_USER + " WHERE " + MyDatabaseHelper.KEY_NAME + " = ' " +personName+" ' "; Sep 12, 2016 at 9:46
  • you have to enclose with quotes Sep 12, 2016 at 9:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.