flurpleplurple
flurpleplurple

Reputation: 1325

Android SQLite database table not being created

I'm following along in an Android for beginner's book and am trying to make a mini app to learn about using SQLite databases. I've built a DataManager class which creates a database and handles the CRUD operations:

  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){

        }
    }
}

However, when I run my app and attempt to insert data, I get an error:

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

I don't know of any way to check how the database has been setup, or how to drop the database so I can recreate it. Any ideas?

Upvotes: 0

Views: 703

Answers (1)

Pooya
Pooya

Reputation: 6136

Use proper spacing before "text" like follows :

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

Upvotes: 1

Related Questions