我对使用SQL存储Android数据项不太熟悉,但是在下面,我有几种方法可以插入新数据项并打印所有现有数据。
我的Flight类具有以下属性:
由于某种原因,我无法打印数据,因此不确定insertFlight()
方法或databaseToString()
方法是否存在问题。
public class DatabaseSQ extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "FlightDatabase.db";
public static final String TABLE_FLIGHTS = "flights";
private static final int DATABASE_VERSION = 1;
public static final String FLIGHT_COLUMN_ID = "_id";
public static final String FLIGHT_NUMBER = "number";
public static final String FLIGHT_DATE = "date";
public static final String FLIGHT_REG = "reg";
public static final String FLIGHT_AIRLINE = "airline";
public static final String FLIGHT_TYPE = "type";
public static final String FLIGHT_IMAGE_PATH = "image";
public DatabaseSQ(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_FLIGHTS + "(" +
FLIGHT_COLUMN_ID + "INTEGER PRIMARY KEY, " +
FLIGHT_NUMBER + " TEXT, " +
FLIGHT_DATE + " TEXT, " +
FLIGHT_REG + " TEXT, " +
FLIGHT_AIRLINE + " TEXT, " +
FLIGHT_TYPE + " TEXT, " +
FLIGHT_IMAGE_PATH + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FLIGHTS);
onCreate(db);
}
public void insertFlight(Flight toADD) {
ContentValues contentValues = new ContentValues();
contentValues.put(FLIGHT_COLUMN_ID, toADD.getId());
contentValues.put(FLIGHT_NUMBER, toADD.getFlightNumber());
contentValues.put(FLIGHT_DATE, toADD.getDate());
contentValues.put(FLIGHT_REG, toADD.getReg());
contentValues.put(FLIGHT_AIRLINE, toADD.getAirline());
contentValues.put(FLIGHT_TYPE, toADD.getType());
contentValues.put(FLIGHT_IMAGE_PATH, toADD.getImagePath());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_FLIGHTS, null, contentValues);
db.close();
}
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_FLIGHTS + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()) {
if(c.getString(c.getColumnIndex("number")) != null) {
dbString += c.getString(c.getColumnIndex("number"));
dbString += "\n";
}
c.moveToNext();
}
db.close();
return dbString;
}
}
在另一个活动文件中,单击一个按钮时,将调用此数据库,必须存储一个新的飞行对象并将其飞行号传递给主活动。 (数据库对象dbHandler之前已实例化。)
public void AddFlight(View view) {
String name = nameText.getText().toString();
String date = dateText.getText().toString();
String airline = airlineText.getText().toString();
String type = typeText.getText().toString();
String reg = regText.getText().toString();
Context context = this;
Class destination = MainActivity.class;
Intent goBackToMain = new Intent(context, destination);
Flight newFlight = new Flight(name, date, reg, airline, type, mCurrentPhotoPath);
dbHandler.insertFlight(newFlight);
String data = dbHandler.databaseToString();
goBackToMain.putExtra(Intent.EXTRA_TEXT, data);
startActivity(goBackToMain);
}
答案 0 :(得分:0)
问题是因为您在表上错误地移动了光标。请按照以下步骤尝试
Cursor c = db.rawQuery(query, null);
if(c != null) {
while(c.moveToNext()) {
dbString += c.getString(c.getColumnIndex("number"));
dbString += "\n";
}
c.close()
}
答案 1 :(得分:0)
FLIGHT_COLUMN_ID + "INTEGER PRIMARY KEY, " +
您在列名称及其类型之间缺少空格。那本身不是语法错误,列名变为_idINTEGER
。
contentValues.put(FLIGHT_COLUMN_ID, toADD.getId());
...
db.insert(TABLE_FLIGHTS, null, contentValues);
由于没有FLIGHT_COLUMN_ID
列,因此此插入将失败。使用insertOrThrow()
而非insert()
引发错误错误。
此外,您无需在INTEGER PRIMARY KEY
列中插入值-将会自动为您生成一个新的rowid。
更正CREATE TABLE
SQL之后,您可以卸载应用程序以使onCreate()
重新运行。