这是我的第一篇文章!希望您能帮帮我!:-)
我使用了NotePadExample提供的来自开发人员文档的代码来创建数据库。现在,我想添加第二个表来存储不同的数据。我可以毫无问题地添加注释,并且注释表的创建方式与路由表类似。我只是简单地“复制”并编辑了给定的代码,但是当我尝试插入到新表中时,出现错误消息:
“ 0ERROR /
Database(370):android.database.sqlite.SQLiteException:没有这样的表:routes:,同时编译:INSERT
INTO routes(行,到达,持续时间,开始)VALUES(?,?,?,?);”
有人可以快速浏览一下我的DbAdapter类,尤其是onCreate()方法。给我一个提示或解决方案?我真的没看到任何问题。我的代码编译没有任何错误。
public class DbAdapter {
public static final String KEY_FROM = "title";
public static final String KEY_TO = "body";
public static final String KEY_ROWID = "_id";
public static final String KEY_START = "start";
public static final String KEY_ARRIVAL = "arrival";
public static final String KEY_LINE = "line";
public static final String KEY_DURATION = "duration";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_NOTESTABLE = "notes";
private static final String DATABASE_ROUTESTABLE = "routes";
private static final String TAG = "DbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE_NOTES =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null)";
private static final String DATABASE_CREATE_ROUTES =
"create table routes (_id integer primary key autoincrement, "
+ "start , arrival , "
+ "line , duration );";
private static final int DATABASE_VERSION = 3;
private final Context mCtx;
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_NOTES);
Log.d(TAG, "created notes table");
db.execSQL(DATABASE_CREATE_ROUTES);
Log.d(TAG, "created routes table");
}
@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 notes");
db.execSQL("DROP TABLE IF EXISTS routes");
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public DbAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the notes database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException if the database could be neither opened or created
*/
public DbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
/**
* Create a new note using the title and body provided. If the note is
* successfully created return the new rowId for that note, otherwise return
* a -1 to indicate failure.
*
* @param title the title of the note
* @param body the body of the note
* @return rowId or -1 if failed
*/
public long createNote(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_FROM, title);
initialValues.put(KEY_TO, body);
return mDb.insert(DATABASE_NOTESTABLE, null, initialValues);
}
/**
* Create a new route using the title and body provided. If the route is
* successfully created return the new rowId for that route, otherwise return
* a -1 to indicate failure.
*
* @param start the start time of the route
* @param arrival the arrival time of the route
* @param line the line number of the route
* @param duration the routes duration
* @return rowId or -1 if failed
*/
public long createRoute(String start, String arrival, String line, String duration){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_START, start);
initialValues.put(KEY_ARRIVAL, arrival);
initialValues.put(KEY_LINE, line);
initialValues.put(KEY_DURATION, duration);
return mDb.insert(DATABASE_ROUTESTABLE, null, initialValues);
}
/**
* Delete the note with the given rowId
*
* @param rowId id of note to delete
* @return true if deleted, false otherwise
*/
public boolean deleteNote(long rowId) {
return mDb.delete(DATABASE_NOTESTABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
/**
* Return a Cursor over the list of all notes in the database
*
* @return Cursor over all notes
*/
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_NOTESTABLE, new String[] {KEY_ROWID, KEY_FROM,
KEY_TO}, null, null, null, null, null);
}
/**
* Return a Cursor over the list of all routes in the database
*
* @return Cursor over all routes
*/
public Cursor fetchAllRoutes() {
return mDb.query(DATABASE_ROUTESTABLE, new String[] {KEY_ROWID, KEY_START,
KEY_ARRIVAL, KEY_LINE, KEY_DURATION}, null, null, null, null, null);
}
/**
* Return a Cursor positioned at the note that matches the given rowId
*
* @param rowId id of note to retrieve
* @return Cursor positioned to matching note, if found
* @throws SQLException if note could not be found/retrieved
*/
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_NOTESTABLE, new String[] {KEY_ROWID,
KEY_FROM, KEY_TO}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Return a Cursor positioned at the route that matches the given rowId
*
* @param rowId id of route to retrieve
* @return Cursor positioned to matching route
* @throws SQLException if note could not be found/retrieved
*/
public Cursor fetchRoute(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_ROUTESTABLE, new String[] {KEY_ROWID,
KEY_START, KEY_ARRIVAL, KEY_LINE, KEY_DURATION}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Update the note using the details provided. The note to be updated is
* specified using the rowId, and it is altered to use the title and body
* values passed in
*
* @param rowId id of note to update
* @param title value to set note title to
* @param body value to set note body to
* @return true if the note was successfully updated, false otherwise
*/
public boolean updateNote(long rowId, String title, String body) {
ContentValues args = new ContentValues();
args.put(KEY_FROM, title);
args.put(KEY_TO, body);
return mDb.update(DATABASE_NOTESTABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
public void deleteRoutesTable(SQLiteDatabase db){
db.delete("routes", null, null);
}
}
我想实现一个像
public void deleteTableRoutes(SQLiteDataBase db){
db.delete("routes", null, null);
}
我的问题:我想从我没有对我的数据库的引用(据我所知)的点调用此函数,以作为我的deleteTableRoutes(SQLiteDataBase)的参数来传递…
代替:
@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 notes");
onCreate(db);
}
我不得不像下面这样修改方法:
@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 notes");
db.execSQL("DROP TABLE IF EXISTS routes"); // line added
onCreate(db);
}
而我不是:
private static final int DATABASE_VERSION = 2;
我将其更改为:
private static final int DATABASE_VERSION = 3;
但是,我并不完全意识到REAL对更改数据库版本产生了影响。
问题内容: 我有数据库,已经创建了一个表,现在 我想在该数据库中创建第二个表()。我的问题是如何在现有数据库中添加此表?我有以下代码。任何帮助表示赞赏。 此代码不会创建第二个表。我想要数据库中的两个表。 它在logcat中显示以下错误。 问题答案: 制作另一个String,然后在您的中,再次调用: 编辑 要将另一个表添加到已存在的数据库中,请按如下所示修改您的方法。每当需要升级数据库时都会调用;请
问题内容: 我有一个非常简单的问题。我需要在绘图上有第二个x轴,并且我希望该轴具有一定数量的tic,它们对应于第一个x轴的特定位置。 让我们尝试一个例子。在这里,我将暗物质质量绘制为膨胀系数(定义为1 /(1 + z))的函数,该膨胀系数的范围为0到1。 我想在图的顶部放置另一个x轴,以显示对应于某些膨胀系数值的z轴。那可能吗?如果是的话,我怎么能拥有xtics斧头 问题答案: 我正在从@Dhar
您好,我无法在数据库中添加多个用户。我想添加我的ArrayList中存在的所有用户。我的代码是: 我的模型是: 因此,我只将第一个用户添加到数据库中,而不将其他用户添加到数据库中。如何在数据库中添加所有用户?
我有一只熊猫df[见下文]。如何将函数中的值添加到新列“price”?
我试图在用户ID下添加新的用户数据,但它将旧数据替换为新数据,并添加另一个“users”和“user\u account\u settings”。 这是我的实时数据库:在此处输入图像描述 我的实时数据库必须是这样的:添加新的用户数据而不将旧数据替换为新数据,也不添加“用户”和“user_account_settings”。 这就是我想要的数据库结构示例:在此处输入图像描述 这是用于添加新用户的代码