我正在尝试使用以下方法从我的数据库中获取值并将其转换为字符串。
private DatabaseHelper dbhelping;
SQLiteDatabase db = dbhelping.getReadableDatabase();
Cursor cursor2 = db.query(TOEDIT, new String[] {"LastEdit"},
null, null, null, null);
String TEMP_DATA = cursor2.getString(2);
我有一个下面格式的数据库。
id2 LastEdit
1 2015-04-29 10:12:54
我想从数据库中抓取2015-04-28 12:27:28并将其转换为字符串,我可以在我的活动中使用它。
我的麻烦是,TOEDIT以红色显示,我不知道如何解决这个问题,或者如果这个方法甚至可以在完成后工作。
如果有人花时间帮助我,我会非常感激。
我的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);
}
}
我的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);
}
}
感谢您的时间。