我正在尝试创建一个包含3个表的数据库,并将数据插入其中一个表中。
数据库和表格创建得非常好,但我无法将数据插入到一个表格中。
DbOperations.java
package com.example.word_a_diction;
import com.example.word_a_diction.TableData.TableInfo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbOperations extends SQLiteOpenHelper {
public static final int Db_Version = 1;
SQLiteDatabase sqldb;
public DbOperations(Context context) {
super(context, TableInfo.Db_Name, null, Db_Version);
System.out.println("Db Operations----Db Created");
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL("CREATE TABLE "+TableInfo.Table1+
" ( "+TableInfo.S_no+" INTEGER PRIMARY KEY
AUTOINCREMENT ,"+TableInfo.Word+" TEXT UNIQUE ,"
+TableInfo.Ans+" TEXT , "+TableInfo.Opt1+" TEXT ,"
+TableInfo.Opt2+" TEXT ,"+TableInfo.Opt3+" TEXT ,"
+TableInfo.Opt4+" TEXT ,"+TableInfo.M1+" TEXT ,"
+TableInfo.M2+" TEXT ,"+TableInfo.M3+" TEXT ,"
+TableInfo.M4+" TEXT );");
System.out.println("db operations-----table1 created");
sdb.execSQL("CREATE TABLE "+TableInfo.Table2+
" ( "+TableInfo.S_no+" INTEGER PRIMARY KEY AUTOINCREMENT, "+TableInfo.Word+" TEXT ,"
+TableInfo.Ans+" TEXT ,"+TableInfo.Opt1+" TEXT ,"
+TableInfo.Opt2+" TEXT ,"+TableInfo.Opt3+" TEXT ,"
+TableInfo.Opt4+" TEXT ,"+TableInfo.M1+" TEXT ,"
+TableInfo.M2+" TEXT ,"+TableInfo.M3+" TEXT ,"
+TableInfo.M4+" TEXT);");
System.out.println("db operations-----table2 created");
sdb.execSQL("CREATE TABLE "+TableInfo.Table3+
" ( "+TableInfo.S_no+" INTEGER PRIMARY KEY AUTOINCREMENT , "+TableInfo.Word+" TEXT ,"
+TableInfo.Ans+" TEXT ,"+TableInfo.Opt1+" TEXT ,"
+TableInfo.Opt2+" TEXT ,"+TableInfo.Opt3+" TEXT ,"
+TableInfo.Opt4+" TEXT ,"+TableInfo.M1+" TEXT ,"
+TableInfo.M2+" TEXT ,"+TableInfo.M3+" TEXT ,"
+TableInfo.M4+" TEXT);");
System.out.println("db operations---table3 created");
insertValues();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TableInfo.Table1+";");
db.execSQL("DROP TABLE IF EXISTS "+TableInfo.Table2+";");
db.execSQL("DROP TABLE IF EXISTS "+TableInfo.Table3+";");
onCreate(db);
}
public void insertValues()
{
//open();
//sqldb = this.getWritableDatabase();
sqldb.execSQL("INSERT INTO BASIC VALUES ('Debonair','Sophisticated,charming','Careful'," +
"'Well-read','Sly','Sophisticated,charming','Using care','Knowledgeable'," +
"'Clever in a dishonest way,Cunning'," +
"'Having a refined knowledge of the ways of the world');");
System.out.println("data inserted");
}
public DbOperations open() {
sqldb = this.getWritableDatabase();
return this;
}
public void close(){
sqldb.close();
}
}
TableData.java
package com.example.word_a_diction;
import android.provider.BaseColumns;
public class TableData {
public TableData()
{
}
public static abstract class TableInfo implements BaseColumns
{
public static final String S_no = "SNo";
public static final String Word = "Word";
public static final String Ans = "Ans";
public static final String Opt1 = "Opt1";
public static final String Opt2 = "Opt2";
public static final String Opt3 = "Opt3";
public static final String Opt4 = "Opt4";
public static final String M1 = "M1";
public static final String M2 = "M2";
public static final String M3 = "M3";
public static final String M4 = "M4";
public static final String Db_Name = "ListOfWords";
public static final String Table1 = "BASIC";
public static final String Table2 = "INTER";
public static final String Table3 = "SKILLED";
}
}
QSkilled.java
package com.example.word_a_diction;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class QSkilled extends Activity
{
private DbOperations db1;
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.q_skilled);
db1 = new DbOperations(this);
db1.open();
}
public void onDestroy(){
db1.close();
}
}
我在日志中不断收到以下错误。一旦我尝试打开 QSkilled.java ,应用程序就会立即关闭。
01-23 09:41:37.875: I/System.out(330): Db Operations----Db Created
01-23 09:41:37.984: I/System.out(330): db operations-----table1 created
01-23 09:41:37.984: I/System.out(330): db operations-----table2 created
01-23 09:41:37.994: I/System.out(330): db operations---table3 created
01-23 09:41:38.048: D/AndroidRuntime(330): Shutting down VM
01-23 09:41:38.054: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-23 09:41:38.076: E/AndroidRuntime(330): FATAL EXCEPTION: main
01-23 09:41:38.076: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.word_a_diction/com.example.word_a_diction.QSkilled}: java.lang.NullPointerException
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-23 09:41:38.076: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 09:41:38.076: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 09:41:38.076: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-23 09:41:38.076: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-23 09:41:38.076: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
01-23 09:41:38.076: E/AndroidRuntime(330): Caused by: java.lang.NullPointerException
01-23 09:41:38.076: E/AndroidRuntime(330): at com.example.word_a_diction.DbOperations.insertValues(DbOperations.java:72)
01-23 09:41:38.076: E/AndroidRuntime(330): at com.example.word_a_diction.DbOperations.onCreate(DbOperations.java:55)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
01-23 09:41:38.076: E/AndroidRuntime(330): at com.example.word_a_diction.DbOperations.open(DbOperations.java:80)
01-23 09:41:38.076: E/AndroidRuntime(330): at com.example.word_a_diction.QSkilled.onCreate(QSkilled.java:102)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-23 09:41:38.076: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-23 09:41:38.076: E/AndroidRuntime(330): ... 11 more
01-23 09:41:42.975: I/Process(330): Sending signal. PID: 330 SIG: 9
答案 0 :(得分:0)
sqldb
中的insertValues()
为空。
由于您想从insertValues()
拨打onCreate()
,因此您无法在那里初始化SQLiteDatabase
。相反,将public void insertValues(SQLiteDatabase sqldb)
作为参数传递并调用其上的方法,例如
{{1}}