我在将值插入我的 Android SQLite 数据库表时遇到问题。我已经尝试过改编一些教程示例,但无济于事。有人可以看看我的代码,指出我应该改变什么才能让它工作吗?这是我班级的代码:
public class Baza extends SQLiteOpenHelper {
SQLiteDatabase db;
static final String dbName ="MyDb";
static final int dbVersion = 1;
// tabela przepisy
static final String TabName = "Przepisy";
static final String TabCol1 = "id";
static final String TabCol2 = "Nazwa";
static final String TabCol3 = "Kategoria";
static final String dbCreate = "CREATE TABLE" + TabName + "(" +
TabCol1+ " INTEGER PRIMARY KEY, "+
TabCol2+ " TEXT, " +
TabCol3+ " TEXT)";
public Baza(Context context) {
super(context, dbName, null, dbVersion);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(dbCreate);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+ TabName);
}
public boolean insert(String name, String cat)
{
db = super.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(TabCol2, name);
cv.put(TabCol3, cat);
long isAdded = db.insert(TabName, null, cv);
if (isAdded == -1) return false;
else return true;
}
}
我会很感激任何帮助。
日志错误:
I/Process(1289): Sending signal. PID: 1289 SIG: 9
D/AndroidRuntime(1318): Shutting down VM
W/dalvikvm(1318): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(1318): FATAL EXCEPTION: main
E/AndroidRuntime(1318): java.lang.NullPointerException
E/AndroidRuntime(1318): at com.example.mojabaza.Baza.insert(Baza.java:60)
E/AndroidRuntime(1318): at com.example.mojabaza.Mojabaza$2.onClick(Mojabaza.java:45)
E/AndroidRuntime(1318): at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(1318): at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime(1318): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(1318): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1318): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(1318): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(1318): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1318): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(1318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(1318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(1318): at dalvik.system.NativeStart.main(Native Method)