未创建Android SQLite数据库表

时间:2016-04-21 14:46:41

标签: android sqlite android-sqlite

我正在关注Android的初学者书,我正在尝试创建一个迷你应用程序来学习使用SQLite数据库。我已经构建了一个DataManager类,它创建一个数据库并处理CRUD操作:

  public class DataManager {

    // this is the actual database
    private SQLiteDatabase db;

    // public static final strings for each row/table we can refer to throughout the app
    public static final String TABLE_ROW_ID = "_id";
    public static final String TABLE_ROW_NAME = "name";
    public static final String TABLE_ROW_AGE = "age";

    // private finals for each row/table we need to refer to just inside this class
    private static final String DB_NAME = "address_book_db";
    private static final int DB_VERSION = 1;
    private static final String TABLE_N_AND_A = "names_and_addresses";

    public DataManager(Context context){
        // create an instance of our internal CustomSQLiteOpenHelper class
        CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);

        db = helper.getWritableDatabase();
    }

    // insert a record
    public void insert(String name, String age){
        // build the query string
        String query = "INSERT INTO " + TABLE_N_AND_A + " (" +
                TABLE_ROW_NAME + ", " +
                TABLE_ROW_AGE + ") " +
                "VALUES (" +
                "'" + name + "'" + ", " +
                "'" + age + "'" +
                ");";
        // log it for reference
        Log.i("insert() = ", query);

        // execute the query
        db.execSQL(query);
    }

    // delete a record
    public void delete(String name){
        String query = "DELETE FROM " + TABLE_N_AND_A +
                " WHERE " + TABLE_ROW_NAME +
                " = '" + name + "';";

        Log.i("delete() = ", query);

        db.execSQL(query);
    }

    public Cursor selectAll() {
        Cursor c = db.rawQuery("SELECT *" + " from " + TABLE_N_AND_A, null);

        return c;
    }

    // search
    public Cursor searchName(String name){
        String query = "SELECT " +
                TABLE_ROW_ID + ", " +
                TABLE_ROW_NAME +
                ", " + TABLE_ROW_AGE +
                " from " +
                TABLE_N_AND_A + " WHERE " +
                TABLE_ROW_NAME + " = '" + name + "';";

        Log.i("searchName = ", query);

        // execute the search and assign the results to a cursor, c
        Cursor c = db.rawQuery(query, null);

        return c;
    }

    // this class is created when our DataManager is initialised
    private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
        public CustomSQLiteOpenHelper(Context context){
            super(context, DB_NAME, null, DB_VERSION);
        }

        // this method only runs the first time the database is created
        @Override
        public void onCreate(SQLiteDatabase db){
            Log.i("info", "DATABASE CREATED");
            // create a table
            String newTableQueryString = "create table "
                    + TABLE_N_AND_A + " ("
                    + TABLE_ROW_ID
                    + " integer primary key autoincrement not null,"
                    + TABLE_ROW_NAME
                    + "text not null,"
                    + TABLE_ROW_AGE
                    + "text not null);";

            db.execSQL(newTableQueryString);
        }

        // this method only runs when we increment the db version
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

        }
    }
}

然而,当我运行我的应用程序并尝试插入数据时,我收到一个错误:

android.database.sqlite.SQLiteException: table names_and_addresses has no column named name (code 1): , while compiling: INSERT INTO names_and_addresses (name, age) VALUES ('adfsd', '23423');

我不知道有什么方法可以检查数据库的设置方式,或者如何删除数据库以便重新创建数据库。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在" text"之前使用适当的间距;如下:

 String newTableQueryString = "create table "
                    + TABLE_N_AND_A + " ("
                    + TABLE_ROW_ID
                    + " integer primary key autoincrement not null,"
                    + TABLE_ROW_NAME
                    + " text not null,"
                    + TABLE_ROW_AGE
                    + " text not null);";

答案 1 :(得分:-1)

private static final String CREATE_DATABASE =
           "create table " + DB_TABLE + " ("+ COLORNAME + " text not null);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public SQLiteAdapter(Context c){
    context = c;
}

public SQLiteAdapter openToRead() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, VERSION);
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this;
}

public SQLiteAdapter openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return this;
}

public void close(){
    sqLiteHelper.close();
}

public long insert(String content){

    ContentValues contentValues = new ContentValues();
    contentValues.put(COLORNAME, content);
    return sqLiteDatabase.insert(DB_TABLE, null, contentValues);
}

public int deleteAll(){
    return sqLiteDatabase.delete(DB_TABLE, null, null);
}

public String queueAll(){
    String[] colors = new String[]{COLORNAME};

    Cursor cursor = sqLiteDatabase.query(DB_TABLE, colors,
            null, null, null, null, null);
    String result = "";

    int index_CONTENT = cursor.getColumnIndex(COLORNAME);
    for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
        result = result + cursor.getString(index_CONTENT) + "\n";
    }

    return result;
}

查看此文章

点击here