我在使用sqlite数据库时遇到了一些问题,但似乎无法找出错误的原因。我有以下课程
public class Greetings {
//private variables
int _id;
String _phone_number;
String _time;
String _date;
String _message;
// Empty constructor
public Greetings(){
}
// constructor
public Greetings(int id, String date, String phone_number, String time, String message){
this._id = id;
this._date = date;
this._phone_number = phone_number;
this._time = time;
this._message = message;
}
// constructor
public Greetings(String date, String phone_number, String time, String message){
this._date = date;
this._phone_number = phone_number;
this._time = time;
this._message = message;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getDate(){
return this._date;
}
// setting name
public void setDate(String date){
this._date = date;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
// getting phone number
public String getTime(){
return this._time;
}
// setting phone number
public void setTime(String time){
this._time = time;
}
public String getMessage(){
return this._message;
}
// setting phone number
public void setMessage(String message){
this._message = message;
}
}
和sqlitehelper类
public class SqliteController extends SQLiteOpenHelper{
private static final String LOGCAT = null;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "sendGreetings";
// table name
private static final String TABLE_GREETINGS = "greetings";
// Greetings Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_DATE = "date";
private static final String KEY_PH_NO = "phone_number";
private static final String KEY_TIME ="time";
private static final String KEY_MESSAGE ="message";
public SqliteController(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(LOGCAT,"Created");
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_GREETINGS_TABLE = "CREATE TABLE " + TABLE_GREETINGS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
+ KEY_PH_NO + " TEXT," + KEY_TIME + "TEXT," + KEY_MESSAGE + "TEXT" + ")";
db.execSQL(CREATE_GREETINGS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GREETINGS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new greetings
void addGreetings(Greetings greetings) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DATE, greetings.getDate()); // Greetings Date
values.put(KEY_PH_NO, greetings.getPhoneNumber()); // Greetings Phone
values.put(KEY_TIME, greetings.getTime()); //Greetings time
values.put(KEY_MESSAGE, greetings.getMessage());
// Inserting Row
db.insert(TABLE_GREETINGS, null, values);
db.close(); // Closing database connection
}
// Getting single greeting
Greetings getGreeting(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_GREETINGS, new String[] { KEY_ID,
KEY_DATE, KEY_PH_NO, KEY_TIME, KEY_MESSAGE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Greetings greeting = new Greetings(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4));
// return contact
return greeting;
}
// Getting All Greetings
public List<Greetings> getAllGreetings() {
List<Greetings> greetingList = new ArrayList<Greetings>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_GREETINGS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Greetings greeting = new Greetings();
greeting.setID(Integer.parseInt(cursor.getString(0)));
greeting.setDate(cursor.getString(1));
greeting.setPhoneNumber(cursor.getString(2));
greeting.setTime(cursor.getString(3));
greeting.setMessage(cursor.getString(4));
// Adding contact to list
greetingList.add(greeting);
} while (cursor.moveToNext());
}
// return greeting list
return greetingList;
}
// Updating single greeting
public int updateGreetings(Greetings greeting) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DATE, greeting.getDate());
values.put(KEY_PH_NO, greeting.getPhoneNumber());
values.put(KEY_TIME, greeting.getTime());
values.put(KEY_MESSAGE, greeting.getMessage());
// updating row
return db.update(TABLE_GREETINGS, values, KEY_ID + " = ?",
new String[] { String.valueOf(greeting.getID()) });
}
// Deleting single greeting
public void deleteGreetings(Greetings greeting) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_GREETINGS, KEY_ID + " = ?",
new String[] { String.valueOf(greeting.getID()) });
db.close();
}
// Getting greetings Count
public int getGreetingsCount() {
String countQuery = "SELECT * FROM " + TABLE_GREETINGS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
每次我通过调用以下代码执行程序时,
SqliteController db = new SqliteController(this);
db.addGreetings(new Greetings(dt,txtPhoneNo,t,n));
我收到错误&#34;表格问候语没有列名为message&#34; 。有人可以帮助我找出我犯错误的地方。提前致谢
答案 0 :(得分:1)
您需要在KEY_MESSAGE
和KEY_TIME
以及TEXT
之间添加一个空格。所以将CREATE_GREETINGS_TABLE
语句重写为
String CREATE_GREETINGS_TABLE = "CREATE TABLE " + TABLE_GREETINGS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
+ KEY_PH_NO + " TEXT," + KEY_TIME + " TEXT," + KEY_MESSAGE + " TEXT" + ")";
不要忘记在重试之前卸载应用程序。