- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个全局 DBAdapter,每个表都有一个。在我的全局 DB-Adapter 中,我将类型“BLOB”添加到列中。但是我不知道我必须在我的 DBAdapter 中为特定表更改什么。我需要改变的是它也适用于 BLOB 类型。
所以你去吧:
全局数据库适配器:
package de.retowaelchli.filterit.database;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import de.retowaelchli.filterit.database.ADFilterDBAdapter;
public class DBAdapter {
public static final String DATABASE_NAME = "filterit";
public static final int DATABASE_VERSION = 1;
public static final String CREATE_TABLE_ADFILTER = "create table adfilter (_id integer primary key autoincrement, "
+ ADFilterDBAdapter.NAME+","
+ ADFilterDBAdapter.KEYWORD+","
+ ADFilterDBAdapter.CACHE + ");";
private static final String CREATE_TABLE_SFILTER = "create table sfilter (_id integer primary key autoincrement, "
+SFilterDBAdapter.NAME+","
+SFilterDBAdapter.KEYWORD+","
+SFilterDBAdapter.SMILEY+ ");";
private static final String CREATE_TABLE_ADMESSAGES = "create table admessages (_id integer primary key autoincrement, "
+MessagesDBAdapter.PHONENUMBER+","
+MessagesDBAdapter.MESSAGE+ ");";
//HERE I CHANGED IT TO BLOB!
private static final String CREATE_TABLE_SMILEY = " create table smiley (_id integer primary key autoincrement, "
+SmileyDBAdapter.SOURCE+" BLOB ,"
+SmileyDBAdapter.INFO+ ");";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
/**
* Constructor
* @param ctx
*/
public DBAdapter(Context ctx)
{
this.context = ctx;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE_ADFILTER);
db.execSQL(CREATE_TABLE_SFILTER);
db.execSQL(CREATE_TABLE_ADMESSAGES);
db.execSQL(CREATE_TABLE_SMILEY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
// Adding any table mods to this guy here
}
}
/**
* open the db
* @return this
* @throws SQLException
* return type: DBAdapter
*/
public DBAdapter open() throws SQLException
{
this.DBHelper = new DatabaseHelper(this.context);
this.db = this.DBHelper.getWritableDatabase();
return this;
}
/**
* close the db
* return type: void
*/
public void close()
{
this.DBHelper.close();
}
}
这是我的这个表的 DBAdapter:
package de.retowaelchli.filterit.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SmileyDBAdapter {
public static final String ROW_ID = "_id";
public static final String SOURCE = "source";
public static final String INFO = "info";
private static final String DATABASE_TABLE = "admessages";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx
* the Context within which to work
*/
public SmileyDBAdapter(Context ctx) {
this.mCtx = ctx;
}
public SmileyDBAdapter open() throws SQLException {
this.mDbHelper = new DatabaseHelper(this.mCtx);
this.mDb = this.mDbHelper.getWritableDatabase();
return this;
}
/**
* close return type: void
*/
public void close() {
this.mDbHelper.close();
}
public long createSmiley(String source, String info ){
ContentValues initialValues = new ContentValues();
initialValues.put(SOURCE, source);
initialValues.put(INFO, info);
return this.mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteSmiley(long rowId) {
return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$
}
public Cursor getAllSmileys() {
return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID,
SOURCE, INFO }, null, null, null, null, null);
}
public Cursor getSmiley(long rowId) throws SQLException {
Cursor mCursor =
this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, SOURCE,
INFO }, ROW_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateSmiley(long rowId, String source, String info,
String cache){
ContentValues args = new ContentValues();
args.put(SOURCE, source);
args.put(INFO, info);
return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;
}
}
我需要在 SmileyDBAdapter 中进行哪些更改才能使其正常工作。它支持 BLOB 类型。尝试了几个小时后,我真的很沮丧 -.-
提前感谢您的回答
最好的问候
Safari
最佳答案
检查此代码以从 Web 链接插入图像并将其作为 BLOB 存储在数据库中并显示在 ImageView 中。
Java 代码:
CategoryDetails.Java
public class CategoryDetails extends Activity {
public NotesDbAdapter mDbHelper;
ByteArrayBuffer baf ;
private Cursor mNotesCursor;
ImageView img;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
new Task_like_flag().execute();
img=(ImageView)findViewById(R.id.ImageView01);
}
public Bitmap convertBlobToBitmap(byte[] blobByteArray) {
Bitmap tempBitmap=null;
if(blobByteArray!=null)
tempBitmap = BitmapFactory.decodeByteArray(blobByteArray, 0, blobByteArray.length);
return tempBitmap;
}
public class Task_like_flag extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(CategoryDetails.this);
JSONObject object_feed;
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(String... params) {
URL url = null;
try {
url = new URL("http://www.theblacksheeponline.com/uploaded/Quick_images/681314276069brewehas.jpg");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //http://example.com/image.jpg
//open the connection
URLConnection ucon = null;
try {
ucon = url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//buffer the download
InputStream is = null;
try {
is = ucon.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(is,128);
baf = new ByteArrayBuffer(128);
//get the bytes one by one
int current = 0;
try {
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mDbHelper.createNote(baf.toByteArray());
return null;
}
@Override
protected void onPostExecute(Void result) {
byte[] imageByteArray;
Bitmap theImage = null;
try{
mNotesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(mNotesCursor);
if (mNotesCursor.moveToFirst()) {
do {
imageByteArray = mNotesCursor.getBlob(mNotesCursor.getColumnIndex(NotesDbAdapter.KEY_IMAGE));
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
theImage= BitmapFactory.decodeStream(imageStream);
} while (mNotesCursor.moveToNext());
}
}catch(Exception e){
Log.v("Excep", ""+e);
}
img.setImageBitmap(theImage);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
}
NotesDbAdapter.Class
public class NotesDbAdapter {
public static final String KEY_CATEGORY = "category";
public static final String KEY_DATE = "notes_date";
public static final String KEY_DESC = "item_desc";
public static final String KEY_PRIZE = "item_prize";
public static final String KEY_MODE = "mode";
public static final String KEY_MONTH = "month";
public static final String KEY_IMAGE = "img";
public static final String KEY_ROWID = "_id";
private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "img BLOB not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public NotesDbAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the notes database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException if the database could be neither opened or created
*/
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
/**
* Create a new note using the title and body provided. If the note is
* successfully created return the new rowId for that note, otherwise return
* a -1 to indicate failure.
*
* @param title the title of the note
* @param body the body of the note
* @return rowId or -1 if failed
*/
public long createNote(byte[] img) {
byte yes[]=img;
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_IMAGE, yes);
Log.v("row", ""+mDb.insert(DATABASE_TABLE, null, initialValues));
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Return a Cursor over the list of all notes in the database
*
* @return Cursor over all notes
*/
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, null, null, null, null, null, null);
} }
主.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
</LinearLayout>
关于android - 如何在 DBAdapter 中创建具有 BLOB 类型列的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7516933/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!