当前位置:首页 » 存储配置 » 存储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是你读出来的字符数组。

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:547
制作脚本网站 发布:2025-10-20 08:17:34 浏览:831
python中的init方法 发布:2025-10-20 08:17:33 浏览:536
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:716
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:639
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:954
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:209
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:67
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:760
python股票数据获取 发布:2025-10-20 07:39:44 浏览:665