当前位置:首页 » 存储配置 » 存储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-02-04 22:39:54 浏览:59
电脑配置低但想玩小偷模拟器怎么办 发布:2025-02-04 22:39:03 浏览:233
最快脚本语言 发布:2025-02-04 22:27:23 浏览:527
安卓的人脸识别在哪里 发布:2025-02-04 22:16:45 浏览:674
悠然服务器的ip是什么 发布:2025-02-04 22:10:17 浏览:65
3des源码 发布:2025-02-04 22:09:16 浏览:809
如何备份数据库表 发布:2025-02-04 22:09:07 浏览:294
如何删除下载的闹钟铃声安卓 发布:2025-02-04 22:03:35 浏览:659
死神脚本 发布:2025-02-04 21:57:03 浏览:168
phpposthtml 发布:2025-02-04 21:37:46 浏览:89