表没有在SQLite中创建

时间:2012-01-27 20:15:05

标签: java android sqlite

我正在尝试创建一个SQLite数据库并最初用数据填充它。当我通过Eclipse中的调试器运行程序时,屏幕上没有任何内容,但我从LogCat获得以下内容:

sqlite returned: error code = 1, msg = no such table: Issues

以下是一些代码(我将尝试仅显示需要的内容,但如果您需要查看更多代码,请告诉我们):

MainActivity.java

public class MainActivity extends Activity {

//TAG for LogCat.
public static final String TAG = "SQLiteDebug";
private ListView mListViewIssues;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Load listview control.
    mListViewIssues = (ListView)findViewById(R.id.listIssues);

    //Create Data Creator.
    IssueInfoCreator creator = new IssueInfoCreator(this);

    creator.open();
    try {
        creator.insertRandomData();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //add data to listview through adapter.
    mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));

    //Close connection.
    creator.close();
}
}

IssueInfoCreator的代码:

public class IssueInfoCreator {

//DB Adapter.
private DBAdapter mDBAdapter;

//Constructor.
public IssueInfoCreator(Context c){
    mDBAdapter = new DBAdapter(c);
}

//Open DBAdapter.
public void open(){
    mDBAdapter.open();
}

//Insert Random Data to get started.
public void insertRandomData() throws ParseException{
    long i = 999;
    Date dtDate = new Date();
    dtDate.parse("1/12/2012");

    mDBAdapter.insertIssue(i++, dtDate, "This is my ticket.  I'm having trouble starting my sim.");
    mDBAdapter.insertIssue(i++, dtDate, "I can't my sim to turn on, please help.");
    mDBAdapter.insertIssue(i++, dtDate, "Aircraft is not responding to callsign.");
    mDBAdapter.insertIssue(i++, dtDate, "TTS is not recognising when we attempt to land an aircraft.");
}

//Get all Issues from DB.
public List<IssueInfo> queryAll(){
    return mDBAdapter.fetchAllIssues();
}

//close connection.
public void close(){
    mDBAdapter.close();
}
}

DBAdapter代码:

public class DBAdapter {

//Database fields.
public static final String ISSUES_TABLE = "Issues";
public static final String COL_ISSUE_ID = "_id";
public static final String COL_DATE_RECEIVED = "DateReceived";
public static final String COL_ISSUE_SUMMARY = "IssueSummary";

//Declarations.
private Context mContext;
private SQLiteDatabase mDB;
private DBHelper mDBHelper;

//Constructor.
public DBAdapter(Context c){
    mContext = c;
}

//Open DB Connection.
public DBAdapter open() throws SQLException{
    mDBHelper = new DBHelper(mContext);
    mDB = mDBHelper.getWritableDatabase();
    return this;
}

//Close DB Connection.
public void close(){
    mDBHelper.close();
}
//remaining code omitted for brevity...
}

最后是DBHelper的代码:

public class DBHelper extends SQLiteOpenHelper {

//Constants
private static final String DB_NAME = "dbOpenIssues";
private static final int DB_VERSION = 1;
private static final String CREATE_DB_SQL = 
        "CREATE TABLE Issues ( " +
                "_id int PRIMARY KEY, " +
                "DateReceived datetime NOT NULL, " +
                "IssueSummary text NOT NULL " +
                ");";
private static final String DROP_TABLE = 
        "DROP TABLE IF EXISTS Issues";

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

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

@Override   
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE);
    onCreate(db);
}
}

如果我使用连接的模拟器运行它,我可以看到数据库是在文件浏览器中创建的,但是如果我在本地将其拉下并用SQLiteMan打开它,它只显示android_metadata表。

有什么想法吗?再次,抱歉这篇长篇文章,只是不想遗漏任何东西,不得不重新发布10次。

更新

我尝试过的一件事是在LogCat中打印SQL语句,但我注意到onCreate(在DBHelper中)永远不会被调用:

public void onCreate(SQLiteDatabase db){
    Log.e("DB ERROR -- ", CREATE_DB_SQL);
    db.execSQL(CREATE_DB_SQL);
}

3 个答案:

答案 0 :(得分:2)

SQLite中没有日期时间类型。要存储数据,您可以使用TEXT。因此,在您的情况下,查询可以是以下内容:

private static final String CREATE_DB_SQL = 
        "CREATE TABLE Issues (" +
                "_id integer PRIMARY KEY, " +
                "DateReceived text NOT NULL, " +
                "IssueSummary text NOT NULL)";

Here是数据类型的链接。

答案 1 :(得分:0)

第一次创建数据库时会调用

onCreate()。因此,如果无法创建表但已创建数据库,则不会再次调用onCreate()

删除数据库,然后使用您所做的任何更改重新运行程序,以查看它是否正常运行...

答案 2 :(得分:0)

您是否尝试使用adb在模拟器中运行SQL Create语句?

如果您不熟悉,请参阅以下有关adb的更多信息:http://developer.android.com/guide/developing/tools/adb.html

在android上调试sqlite问题时,我发现最容易尝试从模拟器内部手动运行SQL命令。