2

实际上,当我创建数据库、我的第一个表(table_userprofile)及其相关字段时,代码运行良好。

但是,一旦我在onCreate()Helper 类的方法下创建了另一个表(table_bloodgroup)。

它开始显示未创建第二个表..并且弹出许多与未创建数据库相关的异常。

以下是代码:

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;
        }
}

记录猫错误:

 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(?);
4

2 回答 2

16

试试这个.... 将数据库版本更改为 2。

私有静态最终 int DATABASE_ VERSION = 2;

于 2012-08-19T13:10:51.913 回答
0

更改版本号会创建一个完整的新数据库模式,包括您的新表。但是,您可以覆盖 SqliteOpenHelper 类的 onUpgrade 方法并编写删除表语句以删除任何现有表或表并使用修改的表列重新创建另一个表。

@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);

    }
于 2020-01-15T11:05:40.307 回答