当前位置:首页 » 存储配置 » 存储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-09-13 14:47:22 浏览:211
源码下载靠谱 发布:2025-09-13 14:27:30 浏览:959
仓库解压码流 发布:2025-09-13 14:20:30 浏览:890
在线编程少儿 发布:2025-09-13 14:19:29 浏览:386
365文档的停止保护密码是多少 发布:2025-09-13 14:04:18 浏览:143
c语言二级编程题 发布:2025-09-13 13:59:09 浏览:837
linux网卡dhcp 发布:2025-09-13 13:58:58 浏览:683
服务器繁忙请重试怎么办 发布:2025-09-13 13:51:05 浏览:46
手机视频怎样压缩最小 发布:2025-09-13 13:20:13 浏览:255
java编程思想第五版 发布:2025-09-13 13:06:08 浏览:409