如何在 Eclipse 中 return 我的 SQLite Table 的第一行
How to return the first row of my SQLite Table in Eclipse
我已经在我的 Android Eclipse 项目中插入了一个 SQLite Table 来存储一些数据,我想 return 我的 SQLite Table 的第一行。
我的数据库 class 是 DataBaseHandler.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "My_SQLite_Database.db";
//constructor for this class
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating the Table when the class is executed
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + "My_SQLite_Database" + "("
+ "Image_ID" + " TEXT,"
+ "Image" + " TEXT," + "Title" + " TEXT," + "Info" + " TEXT" + ")";
db.execSQL(CREATE_TABLE);//executing the create table query
}
//for inserting data into this table write the following method
public void addData(String field1, String field2, String field3, String field4) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Image_ID", field1);
values.put("Image", field2);
values.put("Title", field3);
values.put("Info", field4);
// Inserting Row
db.insert("My_SQLite_Database", null, values);
db.close(); // Closing database connection
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(DataBaseHandler.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + "My_SQLite_Database");
onCreate(db);
}
}
在我的 MainActivity 中,我在我的 onCreate
中初始化我的 SQLite Table(有 4 列和 4 行)
DataBaseHandler db;
db = new DataBaseHandler(getApplicationContext());//Your table is created now
db.addData("d1e14b170ff4418693fd0ceb4987adda","string2","string3","string4");
db.addData("6102a756d92e40d392a4e0ebcb53edb5", "string6", "string7","string8");
db.addData("9d78a74cf93e479f8e08b47072264b66", "string10", "string11","string12");
db.addData("482f894ae06746439d86f8e5efa26914", "string14", "string15","string16");
现在我想在我的 MainActivity 的 onResume 中 return 我的 SQLite Table 的第一行在我 code.What 的特定点我应该做什么?
-->问题 solved.It 很有魅力并检索特定的 SQLite 行<--
我在 DataBaseHandler.java 中插入了一个名为 getContact 的方法来检索特定行,我将其命名为我的 MainActivity。
// ---retrieves a particular contact, written in DataBaseHandler---
public Cursor getContact(String rowId) throws SQLException {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_IMAGEID, KEY_TITLE, KEY_INFO }, KEY_IMAGEID + " LIKE '"
+ rowId + "'", null, null, null, null, null);
if (mCursor != null) {
if (mCursor.getCount() < 1) {
Log.e("GetContact", "cursor empty for " + KEY_IMAGEID + "=\""
+ rowId + "\"");
}
} else
Log.e("GetContact", "cursor null");
return mCursor;
}
//getContact方法的主要Activity调用
public void DisplayContact(Cursor c) {
Toast.makeText(
this,
"Image_ID:" + c.getString(0) + "\n" + "Title:" + c.getString(1)
+ "\n" + "Info:" + c.getString(2), Toast.LENGTH_LONG)
.show();
DataBaseHandler db;
db = new DataBaseHandler(getApplicationContext());// Your table is
// created now
db.open();
Cursor c = db.getContact("24");
System.out.println("Cursor size " + c.getCount());
if (c.moveToFirst()) {
DisplayContact(c); // edo kalei ti methodo DisplayContact pou
// tiponei tin
// 2i grammi tou SQLite Table
} else
Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
我已经在我的 Android Eclipse 项目中插入了一个 SQLite Table 来存储一些数据,我想 return 我的 SQLite Table 的第一行。
我的数据库 class 是 DataBaseHandler.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "My_SQLite_Database.db";
//constructor for this class
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating the Table when the class is executed
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + "My_SQLite_Database" + "("
+ "Image_ID" + " TEXT,"
+ "Image" + " TEXT," + "Title" + " TEXT," + "Info" + " TEXT" + ")";
db.execSQL(CREATE_TABLE);//executing the create table query
}
//for inserting data into this table write the following method
public void addData(String field1, String field2, String field3, String field4) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Image_ID", field1);
values.put("Image", field2);
values.put("Title", field3);
values.put("Info", field4);
// Inserting Row
db.insert("My_SQLite_Database", null, values);
db.close(); // Closing database connection
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(DataBaseHandler.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + "My_SQLite_Database");
onCreate(db);
}
}
在我的 MainActivity 中,我在我的 onCreate
中初始化我的 SQLite Table(有 4 列和 4 行)DataBaseHandler db;
db = new DataBaseHandler(getApplicationContext());//Your table is created now
db.addData("d1e14b170ff4418693fd0ceb4987adda","string2","string3","string4");
db.addData("6102a756d92e40d392a4e0ebcb53edb5", "string6", "string7","string8");
db.addData("9d78a74cf93e479f8e08b47072264b66", "string10", "string11","string12");
db.addData("482f894ae06746439d86f8e5efa26914", "string14", "string15","string16");
现在我想在我的 MainActivity 的 onResume 中 return 我的 SQLite Table 的第一行在我 code.What 的特定点我应该做什么?
-->问题 solved.It 很有魅力并检索特定的 SQLite 行<--
我在 DataBaseHandler.java 中插入了一个名为 getContact 的方法来检索特定行,我将其命名为我的 MainActivity。
// ---retrieves a particular contact, written in DataBaseHandler---
public Cursor getContact(String rowId) throws SQLException {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_IMAGEID, KEY_TITLE, KEY_INFO }, KEY_IMAGEID + " LIKE '"
+ rowId + "'", null, null, null, null, null);
if (mCursor != null) {
if (mCursor.getCount() < 1) {
Log.e("GetContact", "cursor empty for " + KEY_IMAGEID + "=\""
+ rowId + "\"");
}
} else
Log.e("GetContact", "cursor null");
return mCursor;
}
//getContact方法的主要Activity调用
public void DisplayContact(Cursor c) {
Toast.makeText(
this,
"Image_ID:" + c.getString(0) + "\n" + "Title:" + c.getString(1)
+ "\n" + "Info:" + c.getString(2), Toast.LENGTH_LONG)
.show();
DataBaseHandler db;
db = new DataBaseHandler(getApplicationContext());// Your table is
// created now
db.open();
Cursor c = db.getContact("24");
System.out.println("Cursor size " + c.getCount());
if (c.moveToFirst()) {
DisplayContact(c); // edo kalei ti methodo DisplayContact pou
// tiponei tin
// 2i grammi tou SQLite Table
} else
Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}