我正在尝试开发一个物理测验应用程序,但我不断收到错误
02-23 16:02:06.006: E/Database(9348): on sqlite3_open_v2("data/data/com.mcq.srm/databases/q.db", &handle, 1, NULL) failed
下面的代码段
public class QuestionPane extends Activity {
int counter =00;
RadioButton radioButton;
TextView Question;
TextView tvScore;
Button Next;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionpane);
int resdb=0;
try {
SQLiteDatabase checkDB = null;
String DB_FULL_PATH = "data/data/com.mcq.srm/databases/q.db";
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
//Toast.makeText(this,"db "+checkDB, Toast.LENGTH_LONG).show();
resdb=0;
Log.v("msg","Database created");
} catch (Exception e){
resdb=1;
}
Log.v("msg", "check res-->"+resdb);
try{
db = openOrCreateDatabase("q.db" , SQLiteDatabase.CREATE_IF_NECESSARY , null );
if(resdb==1)
{
Log.v("msg","creating tables");
CreateTable();
InsertData();
displayres();
}
}
catch(Exception e){
}
}
public void CreateTable(){
String Createtab;
Createtab =" CREATE TABLE tbl_Question ("+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, Questions TEXT, option_1 TEXT,option_2 TEXT, option_3 Text, option_4 TEXT, correct_answer TEXT);";
try{
db.execSQL(Createtab);
}
catch(Exception e){
}
}
public void InsertData(){
ContentValues values = new ContentValues();
values.put("question", "Two beams of red and violet colours are made to pass separately through a prism of A = 60°. In the minimum deviation position, the angle of refraction inside the prism will be");
//... value.put statements removed
db.insert("tbl_Question", null, values);
//.. values.put statements removed
values.put("correct_answer","35 grams");
db.insert("tbl_Question", null, values);
}
public void displayres() {
int qno=1;
String sql1="select * from question;";
Cursor c1=db.rawQuery(sql1,null);
Log.v("answer","asd");
String que,opt1,opt2,opt3,opt4;
startManagingCursor(c1);
c1.moveToFirst();
que=c1.getString(c1.getColumnIndex("Question1"));
Log.v("answer",que);
}
}
答案 0 :(得分:1)
你有很多错误我建议你通过this示例,最好从android中的那个例子开始。你需要为Database创建单独的类,一般称为数据库适配器和一个这是Android Devs的Database Helper。因此,要获得完整的想法,请通过该示例。