有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在eclipse中使用sql上传和检索图像

我想为用户提供一个选项,上传两张图片,并在保存到数据库后检索图片。我对安卓编程还不熟悉,四天以来我一直在尝试,谷歌搜索了很多,但没有找到确切的解决方案

private SQLiteStatement insertStmt;

private static final String INSERT = "insert into " + TABLE_NAME
        + " (name,number,skypeId,address) values (?,?,?,?)";

public DataManipulator(Context context) {
    DataManipulator.context = context;
    OpenHelper openHelper = new OpenHelper(DataManipulator.context);
    DataManipulator.db = openHelper.getWritableDatabase();
    this.insertStmt = DataManipulator.db.compileStatement(INSERT);

}

public long insert(String name, String number, String skypeId,
        String address) {
    this.insertStmt.bindString(1, name);
    this.insertStmt.bindString(2, number);
    this.insertStmt.bindString(3, skypeId);
    this.insertStmt.bindString(4, address);
    return this.insertStmt.executeInsert();
}

public void deleteAll() {
    db.delete(TABLE_NAME, null, null);
}

public List<String[]> selectAll() {

    List<String[]> list = new ArrayList<String[]>();
    Cursor cursor = db.query(TABLE_NAME, new String[] { "id", "name",
            "number", "skypeId", "address" }, null, null, null, null,
            "name asc");

    int x = 0;
    if (cursor.moveToFirst()) {
        do {
            String[] b1 = new String[] { cursor.getString(0),
                    cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4) };

            list.add(b1);

            x = x + 1;
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    cursor.close();

    return list;
}

public void delete(int rowId) {
    db.delete(TABLE_NAME, null, null);
}

private static class OpenHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "
                + TABLE_NAME
                + " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

}

我的存储数据。java文件

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.save);
    View add = findViewById(R.id.Button01add);
    add.setOnClickListener(this);
    View home = findViewById(R.id.Button01home);
    home.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {

    case R.id.Button01home:
        Intent i = new Intent(this, DatabaseSample.class);
        startActivity(i);
        break;

    case R.id.Button01add:
        View editText1 = (EditText) findViewById(R.id.name);
        View editText2 = (EditText) findViewById(R.id.number);
        View editText3 = (EditText) findViewById(R.id.skypeId);
        View editText4 = (EditText) findViewById(R.id.address);
        String myEditText1 = ((TextView) editText1).getText().toString();` `
        String myEditText2 = ((TextView) editText2).getText().toString();
        String myEditText3 = ((TextView) editText3).getText().toString();
        String myEditText4 = ((TextView) editText4).getText().toString();

        this.dh = new DataManipulator(this);
        this.dh.insert(myEditText1, myEditText2, myEditText3, myEditText4);

        showDialog(DIALOG_ID);
        break;

    }
}

protected final Dialog onCreateDialog(final int id) {
    Dialog dialog = null;
    switch (id) {
    case DIALOG_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Information saved successfully ! Add Another Info?")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                SaveData.this.finish();

                            }
                        })
                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        dialog = alert;
        break;

    default:

    }
    return dialog;
}

}

检查数据。爪哇

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check);
    dm = new DataManipulator(this);
    names2 = dm.selectAll();

    stg1 = new String[names2.size()];

    int x = 0;
    String stg;

    for (String[] name : names2) {
        stg = name[1] + " - " + name[2] + " - " + name[3] + " - " + name[4];

        stg1[x] = stg;
        x++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            安卓.R.layout.simple_list_item_1, stg1);
    this.setListAdapter(adapter);
    selection = (TextView) findViewById(R.id.selection);

}

public void onListItemClick(ListView parent, View v, int position, long id) {
    selection.setText(stg1[position]);
}

}


共 (1) 个答案

  1. # 1 楼答案

    要在数据库表中保存图像,需要将其转换为字节数组,然后将其放入contentValue中。您可以制作一种保存图像的方法,如下所示-

    protected long saveBitmap(SQLiteDatabase database, Bitmap bmp)
    {
        int size = bmp.getRowBytes() * bmp.getHeight(); 
        ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b); 
        byte[] bytes = new byte[size];
        b.get(bytes, 0, bytes.length);
        ContentValues cv=new ContentValues();
        cv.put(CHUNK, bytes);
        this.id= database.insert(TABLE, null, cv);
    }
    

    为了找回那张唱片你可以这样做

    byte[] bb = cursor.getBlob(cursor.getColumnIndex(/*Your column index for saving image*/));