我在SQLite
中创建了一个名为用户的表格,其中包含3列,名称,密码和图片。< / p>
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);
}
}
当我尝试从SQLite
检索图像并放入ImageView
时,我收到错误,但是当我插入数据时,它运行正常。
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);
}
}
});
}
}
错误
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)
alex实际上是我键入的userName,但它没有说明这样的列。
答案 0 :(得分:0)
将您的字符串用单引号括起来,例如
SELECT Image FROM User WHERE Name = 'alex'