开发者

Android: I can not create a db, I get error at: public void onCreate(SQLiteDatabase db)

开发者 https://www.devze.com 2023-03-27 04:29 出处:网络
this is my CallEventAdapter: ` public class CallEventAdapter { public static final StringKEY_ID= \"id\";

this is my CallEventAdapter: ` public class CallEventAdapter {

public static final String      KEY_ID              = "id";

public static final String      KEY_LABEL           = "label";

public static final String      KEY_CONTENT         = "content";

private static final String     TAG                 = "CallAdapter";

private static final String     DATABASE_NAME       = "call_Event";

private static final String     DATABASE_TABLE      = "calls";

private static final int        DATABASE_VERSION    = 1;

private static final String     DATABASE_CREATE     = "create table if not exists calls (id integer primary key, label text not null, content text not nul);";

// private static String DATABASE_PATH =
// "/data/data/fr.intuitiv.app.activity/databases/"; // for now

private final Context           context;

private final DatabaseHelper    DBHelper;

private SQLiteDatabase          db;

public CallEventAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS calEvent");
        onCreate(db);
    }
}

// ---opens the database---
public CallEventAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}

// ---insert a CalEvent into the database---
public long insertCallEvent(long id, String label, String content) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_ID, id);
    initialValues.put(KEY_LABEL, label);
    initialValues.put(KEY_CONTENT, content);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// ---deletes a particular CallEvent---
public boolean deleteCallEvent(long id) {
    return db.delete(DATABASE_TABLE, KEY_ID +开发者_Python百科 "=" + id, null) > 0;
}

// ---retrieves all CallEvents---
public Cursor getAllCallEvents() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ID, KEY_LABEL, KEY_CONTENT }, null, null, null, null, null);
}

// ---retrieves a particular CallEvent---
public Cursor getCallEvent(long id) throws SQLException {
    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { KEY_ID, KEY_LABEL, KEY_CONTENT }, KEY_ID + "=" + id, null, null, null, null, null);
    if(mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

// ---updates a CallEvent---
public boolean updateCallEvent(long id, String label, String content) {
    ContentValues args = new ContentValues();
    args.put(KEY_LABEL, label);
    args.put(KEY_CONTENT, content);
    return db.update(DATABASE_TABLE, args, KEY_ID + "=" + id, null) > 0;
}

}

this is my CallListActivity: public class CallListActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.call_list_events);
    CallEventAdapter db = new CallEventAdapter(this);
    // ---add 2 calEvents---
    db.open();
    long id;
    id = db.insertCalEvent(3, "0470285818", "test 1");
    id = db.insertCalEvent(4, "047017661X", "test2");
    db.close();
}

} `


Check this line:

private static final String     DATABASE_CREATE     = "create table if not exists calls (id integer primary key, label text not null, content text not null);";

The word "null" at the end is incomplete

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号