我有一个奇怪的问题需要解决。系统正确调用SQLiteOpenHelper onCreate()方法,似乎 db.execSQL(sql)成功,但从未创建表。
我使用" adb设备"访问虚拟机>> cd到/ data / data / package / databases>> sqlite3运行.tables。如果我执行"从帖子中选择*"结果是"没有这样的表格。"感谢您提供任何指导。
EDITED
我添加了MainActivity类,以显示我调用了.getWritableDatabase()。当我这样做时,我可以看到onCreate方法被调用,并且没有任何错误,但当我检查表是否存在时......它不是。
DatabaseHelper.java
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DnDa";
public static final String DATABASE_NAME = "db-rss.db";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_TABLE = "posts";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_LINK = "link";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_PUBDATE = "pubdate";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_GUID = "guid";
private Post post;
private ArrayList<Post> posts;
private Cursor cursor;
/**
* constructor
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) throws SQLiteException {
String sql = String.format("CREATE TABLE %s ( %s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT )",
DATABASE_TABLE, COLUMN_ID, COLUMN_TITLE, COLUMN_LINK, COLUMN_DESCRIPTION, COLUMN_PUBDATE, COLUMN_CATEGORY, COLUMN_GUID);
try {
db.execSQL(sql);
Log.d(TAG, "Successfully created table: " + sql);
} catch (SQLiteException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS" + DATABASE_TABLE;
db.execSQL(sql);
this.onCreate(db);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
Log.d(TAG, "Opening database");
}
/**
* Retrieve all records
*/
public Cursor getAllRecords(SQLiteDatabase db) {
db = this.getReadableDatabase();
cursor = db.query(DATABASE_TABLE, new String[] {COLUMN_TITLE, COLUMN_PUBDATE, COLUMN_DESCRIPTION}, null, null, null, null, null);
return cursor;
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private static final String TAG = "DnDa";
private String finalUri = "http://wordpress.com/feed/";
private DatabaseHelper databaseHelper;
private SQLiteDatabase database;
private Post post;
private ArrayList posts;
private ScrollView scrollView;
private ListView listView;
private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
database = databaseHelper.getWritableDatabase();
}
}
答案 0 :(得分:1)
不幸的是,您的表未创建。此SQL Create Table
命令
String sql = "CREATE TABLE "+DATABASE_TABLE+" ( "+
COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+
COLUMN_TITLE+" TEXT,"+
COLUMN_LINK+" TEXT,"+
COLUMN_DESCRIPTION+" TEXT,"+
COLUMN_PUBDATE+" TEXT,"+
COLUMNCOLUMN_CATEGORY+" TEXT,"+
COLUMN_GUID+" TEXT)"
db.execSQL(sql);
答案 1 :(得分:0)
您是否在getWritableDatabase()
对象上致电DatabaseHelper
?可能你有这样的problem吗?
==编辑==
我已使用DatabaseHelper
和Activity
中的此代码创建了一个项目:
DatabaseHelper mOpenHelper = new DatabaseHelper(this);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Cursor с = mOpenHelper.getAllRecords(db);
效果很好,游标包含三列,没有例外。
答案 2 :(得分:0)
确定;我觉得自己像个傻瓜。每次执行代码时都会成功创建表。问题是我在没有指定数据库的情况下运行了“sqlite3”。现在我实际上正在管理正确的数据库,我可以看到该表就在那里。
http://developer.android.com/tools/help/sqlite3.html
sqlite3 /data/data/<package>/databases/<database-name>.db
再次感谢您的贡献和测试我的代码。