目前我有一个应用程序从在线来源获取数据并将此数据转换为String
现在我想比较收到的这个String
数据是否与我数据库中的一行相同。
我不知道如何以可靠的方式做到这一点。
(我想测试String
是否等于2015-04-28 12:27:28
)。
以下是数据库中的数据:
id2 LastEdit
1 2015-04-28 12:27:28
我有一个获取方法
public Cursor fetch2() {
String[] columns = new String[] { DatabaseHelper._ID2, DatabaseHelper.TUT_EDIT };
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME2, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
但我不确定如何在不将其直接转换为R.id.()数组时可靠地获取数据。
编辑:如下文所述
final String[] from = new String[] { DatabaseHelper._ID,
DatabaseHelper.TUT_EDIT};
final int[] to = new int[] { R.id.pid, R.id.edit };
编辑: 包括DBManager
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DBManager {
private DatabaseHelper dbHelper;
private Context context;
private SQLiteDatabase database;
public DBManager(Context c) {
context = c;
}
public DBManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void insert(String id, String cat, String title, String datec, String datem, String name, String image, String video) {
ContentValues contentValue = new ContentValues();
contentValue.put(DatabaseHelper.TAG_TUTID, id);
contentValue.put(DatabaseHelper.TAG_CATID, cat);
contentValue.put(DatabaseHelper.TAG_TUTTITLE, title);
contentValue.put(DatabaseHelper.TAG_NAME, name);
contentValue.put(DatabaseHelper.TAG_DATEC, datec);
contentValue.put(DatabaseHelper.TAG_DATEM, datem);
contentValue.put(DatabaseHelper.TAG_IMAGE, image);
contentValue.put(DatabaseHelper.TAG_VIDEO, video);
database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
Log.d("testing insert: ", cat);
}
public void insert2(String edit) {
ContentValues contentValue = new ContentValues();
contentValue.put(DatabaseHelper.TUT_EDIT, edit);
database.insert(DatabaseHelper.TABLE_NAME2, null, contentValue);
Log.d("testing insert2: ", edit);
}
public Cursor fetch() {
String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.TAG_TUTID, DatabaseHelper.TAG_CATID, DatabaseHelper.TAG_TUTTITLE, DatabaseHelper.TAG_NAME, DatabaseHelper.TAG_DATEC, DatabaseHelper.TAG_DATEM, DatabaseHelper.TAG_IMAGE, DatabaseHelper.TAG_VIDEO };
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public Cursor fetch2() {
String[] columns = new String[] { DatabaseHelper._ID2, DatabaseHelper.TUT_EDIT };
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME2, columns, null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public int update(long _id, String id, String cat, String title, String datec, String datem, String name, String image, String video) {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.TAG_TUTID, id);
contentValues.put(DatabaseHelper.TAG_CATID, cat);
contentValues.put(DatabaseHelper.TAG_TUTTITLE, title);
contentValues.put(DatabaseHelper.TAG_NAME, name);
contentValues.put(DatabaseHelper.TAG_DATEC, datec);
contentValues.put(DatabaseHelper.TAG_DATEM, datem);
contentValues.put(DatabaseHelper.TAG_IMAGE, image);
contentValues.put(DatabaseHelper.TAG_VIDEO, video);
Log.d("testing update: ", image);
int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
return i;
}
public int update2(long _id, String edit) {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.TUT_EDIT, edit);
Log.d("testing update2: ", edit);
int i = database.update(DatabaseHelper.TABLE_NAME2, contentValues, DatabaseHelper._ID2 + " = " + _id, null);
return i;
}
public void delete(long _id) {
database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper._ID + "=" + _id, null);
}
}
AND DBHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
// Table Name
public static final String TABLE_NAME = "TODOS";
// Table columns
public static final String _ID = "_id";
public static final String TAG_TUTID = "tutID";
public static final String TAG_CATID = "catID";
public static final String TAG_TUTTITLE = "tutTitle";
public static final String TAG_NAME = "tutText";
public static final String TAG_DATEC = "tutDateCreated";
public static final String TAG_DATEM = "tutDateModified";
public static final String TAG_IMAGE = "imageID";
public static final String TAG_VIDEO = "videoID";
public static final String TABLE_NAME2 = "TOEDIT";
public static final String _ID2 = "_id2";
public static final String TUT_EDIT = "lastEdit";
// Database Information
static final String DB_NAME = "tuts.DB";
// database version
static final int DB_VERSION = 1;
// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TAG_TUTID + " TEXT NOT NULL, " + TAG_CATID + " TEXT, " + TAG_TUTTITLE+ " TEXT, " + TAG_NAME+ " TEXT, " + TAG_DATEC+ " TEXT, " + TAG_DATEM+ " TEXT, " + TAG_IMAGE+ " TEXT, " + TAG_VIDEO+ " TEXT);";
private static final String CREATE_TABLE2 = "create table " + TABLE_NAME2 + "(" + _ID2
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TUT_EDIT + " TEXT NOT NULL);";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_TABLE);{db.execSQL(CREATE_TABLE2);
Log.d("testing create: ", DB_NAME);}}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
onCreate(db);
Log.d("testing delete: ", DB_NAME);
}
}
感谢您的时间。
答案 0 :(得分:2)
你可以这样做:
Cursor cursor = getDb().query("TABLE_NAME", columns, "field_where = ? AND field_where_2 = ?", new String[]{""+field_1,""+field_2}, null, null, "field_order_by ASC");
if (cursor.getCount()>0){
cursor.moveToFirst();
do{
YourClass d = new YourClass();
d.set_field_1(cursor.getLong(0));
d.set_field_2(cursor.getString(1));
d.set_field_3(cursor.getInt(2));
}while(cursor.moveToNext());
}
这种方式非常可靠。
我希望能帮助你。