【问题标题】:Android SQLite database table not being created未创建 Android SQLite 数据库表
【发布时间】:2016-04-21 14:46:40
【问题描述】:

我正在学习一本面向初学者的 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');

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

【问题讨论】:

    标签: android sqlite android-sqlite


    【解决方案1】:

    在“文本”之前使用适当的间距,如下所示:

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

    【讨论】:

    • @lurpleplurple 具体来说,请注意两个列定义中“文本”一词之前的空格。
    • 我相信,因为我之前运行过该应用程序,数据库已经创建,所以这不会有任何区别吗?如何删除或编辑数据库?
    • @flurpleplurple 您可以卸载该应用并重新安装它
    • 这太简单了。修复成功了!谢谢@pooya。我会在允许的时候接受。
    • 嗨,更好的方法是 changeDATABASE_VERSION。这是工作!
    【解决方案2】:
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多