我正在尝试将两个名为“Friend”和“Social”的对象写入数据库。朋友写没有问题,但'活动'不会工作。
DatabaseHandler代码:
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_FRIENDS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FIRSTNAME + " TEXT," + KEY_LASTNAME + " TEXT,"
+ KEY_EMAIL + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
String CREATE_ACTIVITIES_TABLE = "CREATE TABLE " + TABLE_ACTIVITIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FRIEND + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_DAY + " TEXT" + KEY_MONTH + " TEXT" + KEY_YEAR + " TEXT"+ KEY_FRIEND_ID + " TEXT" +")";
db.execSQL(CREATE_ACTIVITIES_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_FRIENDS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACTIVITIES);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Friend friend) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, friend.getFirstName());
values.put(KEY_LASTNAME, friend.getLastName());
values.put(KEY_EMAIL, friend.getEmail());
// Inserting Row
db.insert(TABLE_FRIENDS, null, values);
db.close(); // Closing database connection
}
// Adding new social
void addSocial(Social social) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FRIEND_ID, social.getFriend_ID());
values.put(KEY_DESCRIPTION, social.getDescription());
values.put(KEY_FRIEND, social.getFriend());
values.put(KEY_DAY, social.getDay());
values.put(KEY_MONTH, social.getMonth());
values.put(KEY_YEAR, social.getYear());
// Inserting Row
db.insert(TABLE_ACTIVITIES, null, values);
db.close(); // Closing database connection
}
// Getting All Contacts
public List<Friend> getAllContacts() {
List<Friend> contactList = new ArrayList<Friend>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_FRIENDS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Friend friend = new Friend();
friend.setId(Integer.parseInt(cursor.getString(0)));
friend.setFirstName(cursor.getString(1));
friend.setLastName(cursor.getString(2));
friend.setEmail(cursor.getString(3));
// Adding contact to list
contactList.add(friend);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Getting All Activities
public List<Social> getAllActivities() {
List<Social> activityList = new ArrayList<Social>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ACTIVITIES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Social social = new Social();
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
social.setId(Integer.parseInt(cursor.getString(0)));
social.setFriend(cursor.getString(1));
social.setDescription(cursor.getString(2));
social.setDay(cursor.getString(3));
social.setMonth(cursor.getString(4));
social.setYear(cursor.getString(5));
social.setFriend_ID(cursor.getString(6));
// Adding contact to list
activityList.add(social);
} while (cursor.moveToNext());
}
return activityList;
}
这是在尝试向DB添加新社交对象的活动中。当我尝试在数据库中显示所有社交对象时,它不会给我和错误,但会显示空白。
Social social = new Social();
List<Friend> friends = db.getAllContacts();
for(Friend friend: friends)
{
if(friend.getFirstName().equalsIgnoreCase(String.valueOf(spinner2.getSelectedItem())))
{
social.setFriend(friend.getFirstName() + friend.getLastName());
social.setFriend_ID(String.valueOf(friend.getId()));
social.setDescription(rs3.getText().toString());
social.setDay(String.valueOf(simpleDatePicker.getDayOfMonth()));
social.setMonth(String.valueOf(simpleDatePicker.getMonth()));
social.setYear(String.valueOf(simpleDatePicker.getYear()));
found = true;
}
}
if(found)
{
db.addActivity(social);
}
答案 0 :(得分:0)
create table SQL中的语法错误。缺少逗号。
答案 1 :(得分:0)
由于省略了逗号,以下代码不会像您期望的那样创建活动表,
String CREATE_ACTIVITIES_TABLE = "CREATE TABLE " + TABLE_ACTIVITIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FRIEND + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_DAY + " TEXT" + KEY_MONTH + " TEXT" + KEY_YEAR + " TEXT"+ KEY_FRIEND_ID + " TEXT" +")";
db.execSQL(CREATE_ACTIVITIES_TABLE);
在KEY_DAY列的列类型(TEXT),KEY_MONTH列和KEY_YEAR列之后,省略了逗号。
相反,它会在的行中创建一个表(注意变量,例如KEY_ID将被相应变量的值替换,这可能会影响结果): -
列: -
因此插入不会以KEY_MONTH列的形式插入行.... KEY_FRIEND_ID将不存在。
更正的代码可能是: -
String CREATE_ACTIVITIES_TABLE =
"CREATE TABLE " + TABLE_ACTIVITIES +
"(" +
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_FRIEND + " TEXT," +
KEY_DESCRIPTION + " TEXT," +
KEY_DAY + " TEXT," + //<<<<<<<< added comma
KEY_MONTH + " TEXT," + //<<<<<<<< added comma
KEY_YEAR + " TEXT," + //<<<<<<<< added comma
KEY_FRIEND_ID + " TEXT" +
")";
db.execSQL(CREATE_ACTIVITIES_TABLE);
更改代码后,您需要执行以下一个(注意数据可能丢失): -
完成上述一次后,您可以重新运行该应用。
请注意,上述代码尚未经过测试,因此可能会出现一些错误。