引起:android.database.sqlite.SQLiteException:表已经存在

时间:2017-03-25 16:37:50

标签: android mysql database sqlite

我的sqlite3数据库中有一个名为Courts的表。根据我的代码,它应该删除表,如果它已经存在,但它不会,并给我一个错误,因为它创建一个已经存在的表。有任何想法吗?提前谢谢。

package com.iu.ajchong.ball_in;

/**
* Created by andrewchong on 3/23/17.
*/

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;

public class CourtsDBHandler extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 5;

    private static final String TABLE_COURTS = "Courts";
    private static final String COLUMN_COURTID_ = "court_id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_STREET = "street";
    private static final String COLUMN_CITY = "city";
    private static final String COLUMN_STATE = "state";
    private static final String COLUMN_ZIP = "zip";


    public CourtsDBHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_COURTS_TABLE = "CREATE TABLE " +
                TABLE_COURTS + "(" +
                COLUMN_COURTID_+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " TEXT, " +
                COLUMN_STREET + " TEXT, " +
                COLUMN_CITY + " TEXT, " +
                COLUMN_STATE + " TEXT, " +
                COLUMN_ZIP + " TEXT)";

        db.execSQL(CREATE_COURTS_TABLE);
        addCourt();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURTS);

        onCreate(db);
    }





    public void addCourt(){
        ContentValues values = new ContentValues();


        ArrayList<ArrayList<String>> myMap = feedDB();


        //iterate through the outer ArrayList and get each ArrayList
        //get the values of each column from the ArrayList
        //System.out.println("Size: "+ myMap.size());
        //System.out.println("first value is " + myMap.get(0).get(0).toString());
        //advanced for loop
        for(ArrayList<String> each : myMap) {
            //System.out.println(each.get(0).toString()+ " is the value from DB");

            values.put(COLUMN_NAME,each.get(0).toString());
            values.put(COLUMN_STREET,each.get(1).toString());
            values.put(COLUMN_CITY,each.get(2).toString());
            values.put(COLUMN_STATE,each.get(3).toString());
            values.put(COLUMN_ZIP,each.get(4).toString());


            SQLiteDatabase db = this.getWritableDatabase();
            try {
                this.onCreate(db);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                db.insert(TABLE_COURTS, null, values);
            } catch (Exception e) {
                e.printStackTrace();
            }
            db.close();
        }





        /* */
    }

    public ArrayList<ArrayList<String>> feedDB(){

        ArrayList<ArrayList<String>> map = new ArrayList<>();

        ArrayList<String> myList = new ArrayList<>();
        ArrayList<String> myList2 = new ArrayList<>();
        ArrayList<String> myList3 = new ArrayList<>();

        myList.add("Briscoe");
        myList.add("1225 N Fee Ln");
        myList.add("Bloomington");
        myList.add("IN");
        myList.add("47408");

        map.add(myList);




        myList2.add("Forest");
        myList2.add("1725 East Third St.");
        myList2.add("Bloomington");
        myList2.add("IN");
        myList2.add("47408");

        map.add(myList2);



        myList3.add("Wright");
        myList3.add("501 N Jordan Ave.");
        myList3.add("Bloomington");
        myList3.add("IN");
        myList3.add("47406");

        map.add(myList3);


        return map;


    }


}

1 个答案:

答案 0 :(得分:4)

递归调用你的onCreate()。特别是,这段代码在一个名为onCreate()

的方法中
SQLiteDatabase db = this.getWritableDatabase();
try {
    this.onCreate(db);
如果需要,

getWritableDatabase()getReadableDatabase()会调用onCreate()。从onCreate()调用的代码不得再次执行此操作。相反,如果您需要,请将您收到的SQLiteDatabase作为参数传递。

你不应该像这样自己打电话给onCreate()。该框架将为您完成。

进一步阅读:When is SQLiteOpenHelper onCreate() / onUpgrade() run?