实际上,当我创建数据库时,代码运行良好,我的第一个表(table_userprofile)及其相关字段。
但是一旦我在Helper Class中的onCreate()
方法下创建了另一个表(table_bloodgroup)。
它开始显示第二个表未创建..并且弹出了许多与未创建数据库相关的异常。
以下是CODE:
package com.example.databaseexample;
import java.util.ArrayList;
import java.util.List;
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 DatabaseHelper extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "bloodDonation";
// Contacts table name
private static final String TABLE_USER="table_userprofile";
private static final String TABLE_BLOODGROUP="table_bloodgroup";
//UserProfile Table Columns names
private static final String KEY_ID="id";
private static final String KEY_NAME="name";
private static final String KEY_PH_NO="phone_number";
private static final String KEY_BGID="id";
private static final String KEY_BNAME="bloodgroup_name";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
context.deleteDatabase(DATABASE_NAME);
// TODO Auto-generated constructor stub
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_UserProfile_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_UserProfile_TABLE);
String createTable_BLOODGROUP="CREATE TABLE IF NOT EXISTS " + TABLE_BLOODGROUP + "( " +
KEY_BGID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
KEY_BNAME + " VARCHAR(30)); ";
db.execSQL(createTable_BLOODGROUP);
Log.d("Created", "Created Both the Tables");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
//db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
//db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOODGROUP);
// Create tables again
//onCreate(db);
}
// **************** Add New UserProfile Entry **********************
void addUser(UserProfile user){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_NAME, user.get_name());
values.put(KEY_PH_NO, user.get_phone_number());
// Inserting Rows
db.insertOrThrow(TABLE_USER, null, values);
db.close();
}
// ***** Retrieve all UserProfile Entry *******
public List<UserProfile> getAllUser(){
List <UserProfile> userList=new ArrayList<UserProfile>();
// Select All Query
String selectQuery="SELECT * FROM " + TABLE_USER;
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.rawQuery(selectQuery, null);
// looping through all rows and adding to the list
if(c.moveToFirst()){
do{
UserProfile user=new UserProfile();
user.set_id(Integer.parseInt(c.getString(0)));
user.set_name(c.getString(1));
user.set_phone_number(c.getString(2));
// Adding user to the list
userList.add(user);
}while(c.moveToNext());
}
c.close();
return userList;
}
// **************** Add BloodGroup Entry **********************
void addBloodGroup(BloodGroup group){
try{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_BNAME, group.get_name());
// Inserting Rows
db.insertOrThrow(TABLE_BLOODGROUP, null, values);
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
// ***** Retrieve all BloodGroups Entry *******
public List<BloodGroup> getAllGroups(){
List <BloodGroup> groupList=new ArrayList<BloodGroup>();
try{
// Select All Query
String selectQuery="SELECT * FROM " + TABLE_BLOODGROUP;
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.rawQuery(selectQuery, null);
// looping through all rows and adding to the list
if(c.moveToFirst()){
do{
BloodGroup grp=new BloodGroup();
grp.set_id(Integer.parseInt(c.getString(0)));
grp.set_name(c.getString(1));
// Adding user to the list
groupList.add(grp);
}while(c.moveToNext());
}
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return groupList;
}
}
LOG CAT ERROR:
08-20 08:36:30.181: I/Database(736): sqlite returned: error code = 1, msg = table table_bloodgroup has no column named bgname
08-20 08:36:30.200: E/Database(736): Error inserting bgname=O
08-20 08:36:30.200: E/Database(736): android.database.sqlite.SQLiteException: table table_bloodgroup has no column named bgname: , while compiling: INSERT INTO table_bloodgroup(bgname) VALUES(?);
答案 0 :(得分:13)
尝试这个...... 将数据库版本更改为2。
私人 静态的 最后 INT 数据库_ 版 = 2;
答案 1 :(得分:0)
更改版本号将创建一个完整的新数据库架构,包括新表。但是,您可以覆盖SqliteOpenHelper类的onUpgrade方法,并编写一个drop table语句以删除任何一个或多个现有表,然后使用已修改的表列重新创建另一个表。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//delete table if table is modified
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//create table again
onCreate(db);
}