这是我第一次设置SQLite数据库,而且我无法弄清楚如何控制光标。让我们说我的专栏名称是(作者,年出版,标题)。并且假设我已经在数据库中保存了一些条目,如:
因此,对于每个书名,都有一个带有书名的按钮(例如," Builder"将出现在用户创建的按钮上)。然后,用户可以单击此按钮,转到另一个活动页面,并且已经放入了这本书的信息。例如,它几乎就像一个新书出现的图书馆" Green Eggs和火腿"由Dr.Seuss撰写,出版时间:1960年。用户浏览我的应用程序的其他部分,输入本书的信息,从而将数据作为条目保存到SQLite数据库中,并创建一个按钮,其中包含" Green Eggs和Ham"在上面。当用户点击此按钮时,它会显示一个包含以下信息的页面:
作者:Dr.Seuss / 发布时间:1960 / 标题:绿鸡蛋和火腿
我的问题是,如何识别我的数据库中的标题,例如" Green Eggs and Ham"然后检索随附的所有其他信息。
我对如何工作的想法:
cursor=DataBase.column("title").StringWithId("GreenEggsAndHam").getRowInformation();
String title=cursor.get("title");
int published=cursor.get("yearPublished");
String author=cursor.get("author")
但显然这不是有效的语法。
我的数据库代码:
public class DataHandler {
public static final String PRIORITY="priority";
public static final String LEASTHOURS="leastHours";
public static final String LEASTMINUTES="leastMinutes";
public static final String EXACTHOURS="exactHours";
public static final String EXACTMINUTES="exactMinutes";
public static final String SETHOURS="setHours";
public static final String SETMINUTES="setMinutes";
public static final String SETAMPM="setAmPm";
public static final String ACTIVITYNAME="activityName";
public static final String TABLE_NAME="mytable";
public static final String DATA_BASE_NAME="mydatabase";
public static final int DATABASE_VERSION=1;
public static final String TABLE_CREATE="create table mytable(name text not null,email text not null);";
DataBaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
this.ctx=ctx;
dbhelper=new DataBaseHelper(ctx);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context ctx){
super(ctx,DATA_BASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(TABLE_CREATE);
}
catch(RuntimeException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
public DataHandler open()
{
db=dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public long insertData(int priority, int leastHours,int leastMinutes,int exactHours,
int exactMinutes,int setHours ,int setMinutes,String setAmPm,String activityName)
{
ContentValues content=new ContentValues();
content.put(PRIORITY, priority);
content.put(LEASTHOURS, leastHours);
content.put(LEASTMINUTES, leastMinutes);
content.put(EXACTHOURS, exactHours);
content.put(EXACTMINUTES, exactMinutes);
content.put(SETHOURS, setHours);
content.put(SETMINUTES, setMinutes);
content.put(SETAMPM, setAmPm);
content.put(ACTIVITYNAME, activityName);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData()
{
return db.query(TABLE_NAME, new String[]{PRIORITY,LEASTHOURS,LEASTMINUTES,
EXACTHOURS,EXACTMINUTES,SETHOURS,SETMINUTES,SETAMPM,ACTIVITYNAME},
null, null, null, null,null);
}
}
答案 0 :(得分:0)
Cursor a = db.rawQuery ("SELECT * FROM MyTable" , null);
a.moveToFirst();
String title = a.getString(a.getColumnIndex("title"));
int published =a.getInt(a.getColumnIndex("yearPublished"));
String author =a.getString(a.getColumnIndex("author"));
a.close();
答案 1 :(得分:0)
您可以使用SQLiteDatabase#query
public Cursor getDetailsForBook(String title) {
return db.query(TABLE_NAME, new String[] {"title", "yearPublished","author"},
"title like " + "'%" + title +"%'", null, null, null, null);
}
然后阅读详细信息:
Cursor cursor = dbhandler.getDetailsForBook("Green Eggs and Ham");
String title = cursor.getString(cursor.getColumnIndex("title"));
int published = cursor.getInt(cursor.getColumnIndex("yearPublished"));
String author = cursor.getString(cursor.getColumnIndex("author"));
希望这有帮助。
答案 2 :(得分:0)
添加一些检查并提高搜索效率
db扩展了SQLiteDatabase类
String title = null;
int published=0;
String author = null;
Cursor cur=db.rawQuery("SELECT * FROM people WHERE title = ?", new String[] {"Green Eggs and Ham"});
if(cur.getCount() > 0){
title = cur.getString(a.getColumnIndex("title"));
published =cur.getInt(a.getColumnIndex("yearPublished"));
author =cur.getString(a.getColumnIndex("author"));
cur.close();
}