android sqlite表创建错误

pxyaymoc  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(326)

我有我的代码创建sqlite数据库,代码创建数据库但不创建任何表。我经历了一些类似的错误,其他人之前,我找不到任何错误。有人能帮我吗。
这是我的密码

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;

    }

}

这是活动课的主题

public class Register extends AppCompatActivity {

DBHelper repo = new DBHelper(this);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        countries = getResources().getStringArray(R.array.countries);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, countries);
        autoCompleteTextView.setAdapter(adapter);

        insertDummyData();
        loadProvince_Simple();

    }

// This class insert data
private void insertDummyData(){

        repo.DeleteProvice();
        int i = 1;
        String[] provinces = {"Prince Edward Island", "Quebec", "Saskatchewan", "Yukon","Northwest Territories", "Ontario", "Nunavut", "Nova Scotia", "Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland and Labrador"};

        for(int j=0; j < provinces.length; j++) {
                Province province = new Province();
                province.setId(Integer.toString(i));
                province.setProvince(provinces[j]);

                repo.insertProvince(province);
            i++;
        }

    }

//This is the arrayadapter  binding method as you can see
    private void loadProvince_Simple(){
        ArrayAdapter<String> spinnerAdapter;
        DBHelper db = new DBHelper(getApplicationContext());
        List<String> provinces = db.getAll_Simple();
        spinnerAdapter = new ArrayAdapter<String>(Register.this,
                android.R.layout.simple_spinner_item, provinces);
        prov_spinner.setAdapter(spinnerAdapter);

        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    }
}
ve7v8dk2

ve7v8dk21#

请阅读我的评论:

// 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 );";

相关问题