2
我有問題,在Android中的數據庫訪問。 首先,我創建了一個用於在數據庫中創建表的活動類。 然後我從另一個方面調用了該活動。現在在編譯時,我遇到了表不存在的錯誤。這是代碼。「表不存在」與sqlite的錯誤
第一類是: -
package bivin.hello;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class main1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i=new Intent();
startActivity(i);
SQLiteDatabase db;
db=(SQLiteDatabase)openOrCreateDatabase("bivin.db", SQLiteDatabase.OPEN_READONLY,null);
Cursor cur=db.query("emp", null, null, null, null,null,null);
db.beginTransaction();
cur.moveToPosition(1);
db.close();
和調用活動
package com.example.android.apis;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class createtable extends Activity
{
public void onCreate(Bundle b)
{
super.onCreate(b);
SQLiteDatabase db;
db=openOrCreateDatabase("bivin.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("create table emp(name text,password text;");
ContentValues c=new ContentValues();
c.put("name", "Bivin");
c.put("password", "Ravindran");
db.insert("emp",null,c);
db.close();
}
}
}
}
我只是想從一個活動創建一個數據庫表,然後從另一個訪問它。 – 2011-05-28 06:45:03
'createtable.onCreate'在'main1.onCreate'之前運行?如果情況並非如此,它將會失敗。 – 2011-05-28 06:46:55
no here main1.onCreate首先運行。你能告訴我只是有點困惑的原因:-)。 – 2011-05-28 06:50:00