SQLite的 - 是否有可能通过插入INSERT语句中的BLOB? [英] SQLite - Is it possible to insert a BLOB via insert statement?

查看:150
本文介绍了SQLite的 - 是否有可能通过插入INSERT语句中的BLOB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我使用SQLite数据库来存储一些位图。我想一些图像,当用户安装该应用程序自动插入。

I'm developing an Android application and i'm using a Sqlite database to store some bitmaps. I want some images to be automatically inserted when the user installs the application.

我使用的 SQLiteOpenHelper 类是这样的:

public class DatabaseHelper extends SQLiteOpenHelper {

...

DatabaseHelper(Context context, String nameOfDB, int version, String[] scriptSQLCreate,
        String scriptSQLDelete) {
    super(context, nameOfDB, null, version);

    this.scriptSQLCreate = scriptSQLCreate;
    this.scriptSQLDelete = scriptSQLDelete;
}

@Override
public void onCreate(SQLiteDatabase db) {
    int numScripts = scriptSQLCreate.length;
    for(int i = 0; i<numScripts; i++){
    Log.i(TAG,"Creating database, executing script " + i);
    db.execSQL(scriptSQLCreate[i]);
    }
}
}

...

我要一个恒定的传递到上面所显示的scriptSQLCreate参数会像这样:

I want to pass a constant to the scriptSQLCreate parameter shown above that would be like so:

private static final String[] SCRIPT_DATABASE_CREATE = {
   "create table memes(  id integer primary key autoincrement," + 
                     + " img blob not null," + 
                     + " name text not null unique)" ,
   "insert into memes(img,name) values(BITMAP1,'1.jpg')",
   "insert into memes(img,name) values(BITMAP2,'2.jpg')",
   "insert into memes(img,name) values(BITMAP3,'3.jpg')"}    

}

任何帮助将是多大的preciated,

Any help will be much apreciated,

THX,
图利奥赞恩。

Thx, Tulio Zahn

推荐答案

如果你真的想你可以使用一个很长的十六进制文本作为一个blob文字:

If you really, really want to you can use a very long hex literal as a blob literal:

insert into memes(img, name) values(X'0102030405060708090a0b0c0d0e0f', '1.jpg')

不过,这通常是一个坏主意;相反,去看看<一个href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#compileStatement%28java.lang.String%29\"相对=nofollow>参数化查询的。他们会让你编译一个语句中多次使用占位符代替实际值,然后再用了很多次,在填写的占位符需要:

However, this is usually a bad idea; instead, go look at parameterised queries. They will let you compile a statement once using placeholders instead of actual values, and then reuse it many times, filling in the placeholders as needed:

SQLiteStatement p = sqlite.compileStatement("insert into memes(img, name) values(?, ?)");

byte[] data = loadData("1.jpg");
p.bindBlob(1, data);
p.bindString(2, "1.jpg");
p.execute();

byte[] data = loadData("2.jpg");
p.bindBlob(1, data);
p.bindString(2, "2.jpg");
p.execute();

(警告--- code未经测试。)

(Warning --- code not tested.)

在一般你应该使用参数化查询处处的,因为他们是一个安全可靠的方式,以避免SQL注入攻击,再加上通常更容易,更清晰。通过胶合在一起的字符串应该不惜一切代价避免组装SQL查询。

In general you should be using parameterised queries everywhere, as they're a sure-fire way to avoid SQL injection attacks, plus are usually easier and clearer. Assembling SQL queries by glueing strings together should be avoided at all costs.

这篇关于SQLite的 - 是否有可能通过插入INSERT语句中的BLOB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆