SQLite - 在创建数据库时获取 FC
忽略这个问题,我发现了我的问题...只是编辑了问题以包含正确的代码,以防万一有人寻找正确的数据库示例:D
每次运行它时,我的应用程序都会关闭。它仅在创建数据库时进行 FC,但如果我在创建数据库时注释该行,则应用程序运行正常。 (请参阅下面的代码以供参考)
如果我导航到 /data/data/com.mypackage/databases/ 数据库实际上在那里,它就会被创建,甚至是带有元组的表,因此,我相信我的 db 类是美好的。我一定在主类中做错了什么(也许不是??)
主类,这是我实际创建数据库对象
public class myClass extends MapActivity {
DataHelper dbh; // declaring database
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// =======
// more code here
// =======
this.dbh = new DataHelper(this); // this line gives my app an FC, if I comment this line my app runs fine
}
// =======
// more code here
// =======
}
数据库类的地方:
public class DataHelper {
private static final String DATABASE_NAME = "track.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "info";
private Context context;
private SQLiteDatabase db;
private static final String
sportsType = "sType",
units = "units",
date = "date",
tTime = "tTime";
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_NAME + "(" + sType + ", " +
units + ", " +
date + ", " +
tTime + ") values (?,?,?,?)";
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public long insert(String sType, String units, String date, String tTime) {
this.insertStmt.bindString(1, sType);
this.insertStmt.bindString(2, units);
this.insertStmt.bindString(3, date);
this.insertStmt.bindString(4, tTime);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { sType,
units,
date,
tTime },
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
list.add(cursor.getString(1));
list.add(cursor.getString(2));
list.add(cursor.getString(3));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(_id INTEGER PRIMARY KEY, " +
sType + " TEXT, " +
units + " TEXT, " +
date + " TEXT, " +
tTime + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
你们认为我做错了什么?
谢谢 :)
DISREGARD this question, I found my problem...just edited the question to include the correct code jut in case someone looks for a correct database sample :D
My application force closes every time I run it. It only FCs when the database gets created, but if I comment the line when I'm creating the database, the app runs fine. (See code below for reference)
If I navigate to /data/data/com.mypackage/databases/ the database is actually there, it gets created, even the table with its tuples, so because of this, I believe my db class is fine. I must be doing something wrong in the main class (maybe not??)
main class, this is where I actually create the database object
public class myClass extends MapActivity {
DataHelper dbh; // declaring database
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// =======
// more code here
// =======
this.dbh = new DataHelper(this); // this line gives my app an FC, if I comment this line my app runs fine
}
// =======
// more code here
// =======
}
database class:
public class DataHelper {
private static final String DATABASE_NAME = "track.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "info";
private Context context;
private SQLiteDatabase db;
private static final String
sportsType = "sType",
units = "units",
date = "date",
tTime = "tTime";
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_NAME + "(" + sType + ", " +
units + ", " +
date + ", " +
tTime + ") values (?,?,?,?)";
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public long insert(String sType, String units, String date, String tTime) {
this.insertStmt.bindString(1, sType);
this.insertStmt.bindString(2, units);
this.insertStmt.bindString(3, date);
this.insertStmt.bindString(4, tTime);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { sType,
units,
date,
tTime },
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
list.add(cursor.getString(1));
list.add(cursor.getString(2));
list.add(cursor.getString(3));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(_id INTEGER PRIMARY KEY, " +
sType + " TEXT, " +
units + " TEXT, " +
date + " TEXT, " +
tTime + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
What do you guys think I'm doing wrong?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论