當前位置:首頁 » 存儲配置 » 存儲byte數組中

存儲byte數組中

發布時間: 2023-06-20 07:39:06

㈠ Android 怎樣用sqlite儲存byte數組

在進行Android開發過程中,我們經常會接觸到Drawable對象(官方開發文檔:A Drawable is a general abstraction for "something that can be drawn."),那麼,若要使用資料庫來進行存儲及讀取.

@Override
public void onCreate(SQLiteDatabase database) {
executeSQLScript(database, "create.sql");
}

private void executeSQLScript(SQLiteDatabase database, string dbname){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;

try{
inputStream = assetManager.open(dbname);
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
String[] createScript = outputStream.toString().split(";");
for (int i = 0; i < createScript.length; i++) {
String sqlStatement = createScript[i].trim();
// TODO You may want to parse out comments here
if (sqlStatement.length() > 0) {
database.execSQL(sqlStatement + ";");
}
}
} catch (IOException e){
// TODO Handle Script Failed to Load
} catch (SQLException e) {
// TODO Handle Script Failed to Execute
}
}

㈡ 在java中如何把位元組數組存儲到資料庫

保存位元組數組到資料庫分兩步:
第一、利用FileInputStream.read(byte[])方法把內容讀取到byte[]數組中,比如圖片是由二進制數組成的,就可以定義為一個位元組數組。
第二、在資料庫中對應記錄欄位應該設置為blob類型,這樣就能夠順利保存了
事例代碼如下:
PreparedStatement stmt = connection.generatePreparedStatement("INSERT INTO ... ");
stmt.setBytes(1, yourByteArray);

其中,yourByteArray是你讀出來的字元數組。

熱點內容
編程里的等於 發布:2025-03-20 16:22:02 瀏覽:916
ise綜合與編譯 發布:2025-03-20 16:13:22 瀏覽:880
linux文件句柄 發布:2025-03-20 16:11:55 瀏覽:636
如來神掌原始登錄密碼多少 發布:2025-03-20 16:06:32 瀏覽:319
保電訪問 發布:2025-03-20 16:06:23 瀏覽:144
08年雅閣都有什麼配置 發布:2025-03-20 15:55:48 瀏覽:909
塗鴉解壓法 發布:2025-03-20 15:54:54 瀏覽:677
三國伺服器什麼時候開放 發布:2025-03-20 15:50:43 瀏覽:657
英詩派2022款大改款選哪個配置好 發布:2025-03-20 15:36:50 瀏覽:909
廈門如何找回中招報名的密碼 發布:2025-03-20 15:30:56 瀏覽:959