java-如何优化将具有多个字符串和图像的条目插入到SQLite数据库中

java-如何优化将具有多个字符串和图像的条目插入到SQLite数据库中,第1张

概述我定义了一种方法,可以在数据库帮助程序类中添加一个条目,并使用该条目将数据插入数据库中,但是这种方法不起作用.这是我在databasehelper类中定义的方法:publicvoidcreatechannelEntry(ChannelPosterchannel){openDB();ByteArrayOutputStreamout=newB

我定义了一种方法,可以在数据库帮助程序类中添加一个条目,并使用该条目将数据插入数据库中,但是这种方法不起作用.这是我在databasehelper类中定义的方法:

 public voID createchannelEntry(ChannelPoster channel) {        openDB();        ByteArrayOutputStream out = new ByteArrayOutputStream();        channel.getPoster().compress(Bitmap.CompressFormat.PNG, 100, out);        ContentValues cv = new ContentValues();        cv.put(KEY_POSTER, out.toByteArray());                    cv.put(KEY_CHANNEL, channel.getChannel());        cv.put(KEY_PATH, channel.getPath());        cv.put(KEY_DBlink, channel.getDBlink());        mDb.insert(channelS_table, null, cv);        closeDB();    }

这就是我插入数据的方式

Bitmap sherlock = BitmapFactory.decodeResource(getResources(), R.drawable.sherlock);mDB.createchannelEntry(new ChannelPoster(sherlock, "aa" ,"ll"  ,"ha" ));

而且我有一个JavaBean用于保存条目

public class ChannelPoster {    private Bitmap poster;    private String channel;    private String path;    private String dblink;    public ChannelPoster(Bitmap pi, String c, String p, String d) {        poster = pi;        channel = c;        path = p;        dblink = d;    }    public Bitmap getPoster() { return poster; }    public String getChannel() { return channel; }    public String getPath() { return path; }    public String getDBlink() { return dblink; }}

而且由于我要一个接一个地添加条目,所以程序运行非常慢,所以有插入多个条目的较快方法吗?想让所有人都参加一次活动吗?

解决方法:

我建议不要将图像保存在数据库中,而是将它们保存为文件,并在数据库中保存它们的路径(使用普通的TEXT字段).

如果您不想这样做,那么有两点仍然可以极大地提高处理速度:

>围绕所有插入仅打开和关闭数据库一次
>使用交易

像这样:

sqliteDatabase db = openDB();db.beginTransaction();try {    // Add here the loop with all your inserts    db.setTransactionSuccessful();} finally {    db.endTransaction();  // will rollback and cancel the inserts if not marked as successful    db.close();}

(最后可以确保关闭事务和连接,即使在插入过程中由于某种原因导致连接失败:在这种情况下,您所有的插入都将被取消)

总结

以上是内存溢出为你收集整理的java-如何优化将具有多个字符串和图像的条目插入到SQLite数据库中全部内容,希望文章能够帮你解决java-如何优化将具有多个字符串和图像的条目插入到SQLite数据库中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1084220.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存