从数据库中获取单个值并转换为字符串

时间:2022-04-03 12:09:48

At the moment i have a app that get data from a online source and converts this data to String

目前我有一个应用程序从在线源获取数据并将此数据转换为String

Now i want compare if this String data received is the same as one row in my database.

现在我想要比较收到的String数据是否与我的数据库中的一行相同。

I don't know how can i do it as the reliable way.

我不知道我怎么能以可靠的方式做到这一点。

(i want to test if the String is equals to 2015-04-28 12:27:28).

(我想测试String是否等于2015-04-28 12:27:28)。

Here is the data in the database:

这是数据库中的数据:

id2 LastEdit
1   2015-04-28 12:27:28

i have got a fetch methord

我有一个获取方法

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;
    }

but im not sure how to grab the data reliably when im not converting it straight into a R.id.() array.

但我不确定如何在不将其直接转换为R.id.()数组时可靠地获取数据。

EDIT: as in the bellow

编辑:如在下面

final String[] from = new String[] { DatabaseHelper._ID,
            DatabaseHelper.TUT_EDIT};


    final int[] to = new int[] { R.id.pid, R.id.edit };

EDIT: including DBManager

编辑:包括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);
}
}

Thank you for your time.

感谢您的时间。

1 个解决方案

#1


You can do this:

你可以这样做:

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());
        }

This way is very reliable.

这种方式非常可靠。

I hope help you.

我希望能帮助你。

#1


You can do this:

你可以这样做:

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());
        }

This way is very reliable.

这种方式非常可靠。

I hope help you.

我希望能帮助你。