Android SQLite表创建错误

时间:2017-01-03 14:17:02

标签: java android sqlite android-sqlite

我有我的代码来创建SQLite数据库,代码创建数据库但不创建任何表。我经历了其他人之前遇到的一些类似的错误,我找不到任何错误。任何人都可以帮助我。

这是我的代码

\n

这是活动类

public class DBHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "swahilComm.db";

    public static final String COUNTRY_TABLE = "countries";
    public static final String COUNTRY_ID = "id";
    public static final String COUNTRY_NAME = "country";

    public static final String PROVINCE_TABLE = "province";
    public static final String PROVINCE_ID = "id";
    public static final String PROVINCE_NAME = "province";

    public static final String CREATE_TABLE_PROVINCE = "CREATE TABLE " + PROVINCE_TABLE  + "("
            + PROVINCE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + PROVINCE_NAME + " TEXT );";

    public static final String CREATE_COUNTRY_TABLE = "CREATE TABLE " + COUNTRY_TABLE + "("
            + COUNTRY_ID + " INTEGET PRIMARY KEY AUTOINCREMENT,"
            + COUNTRY_NAME + " TEXT );";

    public DBHelper(Context context ) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("Database Operation", "Database Created...");
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_TABLE_PROVINCE);
        db.execSQL(CREATE_COUNTRY_TABLE);

        Log.d("Database Operation", "Tables Created...");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed, all data will be gone
        db.execSQL("DROP TABLE IF EXISTS " + PROVINCE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + COUNTRY_TABLE);

        // Create tables again
        onCreate(db);

    }

    //Delete all data in the table
    public void DeleteCountry() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(COUNTRY_TABLE, null, null);
        db.close(); // Closing database connection
    }

    //Delete all data in the Province table
    public void DeleteProvice() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(PROVINCE_TABLE, null, null);
        db.close(); // Closing database connection
    }


    //Insert country records
    public int insertCountry(Country country) {
        // TODO Auto-generated method stub
        //Integer noProvince = getProvinceCount();

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COUNTRY_ID, country.getId());
        values.put(COUNTRY_NAME, country.getCountry());

        // Inserting Row
        long country_Id = db.insert(COUNTRY_TABLE, null, values);
        db.close(); // Closing database connection
        return (int) country_Id;
    }


    //Insert province records
    public int insertProvince(Province province) {
        // TODO Auto-generated method stub
        //Integer noProvince = getProvinceCount();

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(PROVINCE_ID, province.getId());
        values.put(PROVINCE_NAME, province.getProvince());

        // Inserting Row
        long province_Id = db.insert(PROVINCE_NAME, null, values);
        db.close(); // Closing database connection
        return (int) province_Id;
    }

    //Retrieve all records and populate into List<Country>
    //This method allow you to retrieve more fields/information into List.
    public List<Country> getAllCountry() {
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT  " +
                COUNTRY_ID + "," +
                COUNTRY_NAME +
                " FROM " + COUNTRY_TABLE;

        List<Country> countryList = new ArrayList<Country>() ;
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Country country = new Country();
                country.setId(cursor.getString(cursor.getColumnIndex(COUNTRY_ID)));
                country.setCountry(cursor.getString(cursor.getColumnIndex(COUNTRY_NAME)));
                countryList.add(country);

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return countryList;

    }


    //Retrieve all records and populate into List<Province>
    //This method allow you to retrieve more fields/information into List.
    public List<Province> getAll() {
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT  " +
                PROVINCE_ID + "," +
                PROVINCE_NAME +
                " FROM " + PROVINCE_TABLE;

        List<Province> provinceList = new ArrayList<Province>() ;
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Province province  = new Province();
                province.setId(cursor.getString(cursor.getColumnIndex(PROVINCE_ID)));
                province.setProvince(cursor.getString(cursor.getColumnIndex(PROVINCE_NAME)));
                provinceList.add(province);

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return provinceList;

    }


    //Retrieve all records and populate into List<String>
    public List<String> getAllStringCountry() {
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT  " +
                COUNTRY_ID + "," +
                COUNTRY_NAME +
                " FROM " + COUNTRY_TABLE;

        List<String> countryList = new ArrayList<String>() ;
        Cursor cursor = db.rawQuery(selectQuery, null);
        Integer i=0;
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                countryList.add(i,cursor.getString(cursor.getColumnIndex(COUNTRY_NAME)));
                i+=1;
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return countryList;

    }


    //Retrieve all records and populate into List<String>
    public List<String> getAll_Simple() {
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT  " +
                PROVINCE_ID + "," +
                PROVINCE_NAME +
                " FROM " + PROVINCE_TABLE;

        List<String> provinceList = new ArrayList<String>() ;
        Cursor cursor = db.rawQuery(selectQuery, null);
        Integer i=0;
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                provinceList.add(i,cursor.getString(cursor.getColumnIndex(PROVINCE_NAME)));
                i+=1;
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return provinceList;

    }

}

1 个答案:

答案 0 :(得分:0)

请阅读以下评论:

// TRY TO REMOVE THE SPACE BETWEEN "TEXT" TO THE "(" IN THE LAST LINE OF BOTH STRINGS
public static final String CREATE_TABLE_PROVINCE = "CREATE TABLE " + PROVINCE_TABLE  + "("
            + PROVINCE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + PROVINCE_NAME + " TEXT );";


// YOU WROTE "INTEGET"
public static final String CREATE_COUNTRY_TABLE = "CREATE TABLE " + COUNTRY_TABLE + "("
            + COUNTRY_ID + " INTEGET PRIMARY KEY AUTOINCREMENT,"
            + COUNTRY_NAME + " TEXT );";