Android SQLite:INSERT detailMessage null

时间:2013-02-06 16:10:36

标签: android sqlite

我尝试从db_insert.java插入数据并按MyDB.java中的方法处理。

public void append(String n,int p) { 
      String str = "INSERT INTO " + TABLE_NAME + " (" + NAME + "," + PRICE + ") VALUES (" + n +"," + p + ")";
        db.execSQL(str);      
}

在调试模式下,n& pdb_insert.java获取数据。

到目前为止,没关系。但是当进程db.execSQL(str);时,应用程序将崩溃。

有人知道为什么吗?

logcat的

    02-07 00:16:48.467: I/Process(979): Sending signal. PID: 979 SIG: 9 02-07 00:17:17.827:

 E/MyDBlog(1109): click insert_OKs 02-07 00:17:17.827: D/AndroidRuntime(1109): Shutting 


down VM 02-07 00:17:17.827: W/dalvikvm(1109): threadid=1: thread exiting with uncaught 

exception (group=0x2aac8578) 02-07 00:17:17.827: E/AndroidRuntime(1109): FATAL EXCEPTION: 

main 02-07 00:17:17.827: E/AndroidRuntime(1109): java.lang.NullPointerException 02-07 

00:17:17.827: E/AndroidRuntime(1109): at com.example.mydb.MyDB.append(MyDB.java:39) 02-07 

00:17:17.827: E/AndroidRuntime(1109): at com.example.mydb.db_insert$1.onClick(db_insert.java:66) 02-07 00:17:17.827: 

E/AndroidRuntime(1109): at android.view.View.performClick(View.java:2552) 02-07 

00:17:17.827: E/AndroidRuntime(1109): at android.view.View$PerformClick.run(View.java:9229) 

02-07 00:17:17.827: E/AndroidRuntime(1109): at android.os.Handler.handleCallback(Handler.java:587) 02-07 00:17:17.827: 

E/AndroidRuntime(1109): at android.os.Handler.dispatchMessage(Handler.java:92) 02-07 

00:17:17.827: E/AndroidRuntime(1109): at android.os.Looper.loop(Looper.java:130) 02-07 

00:17:17.827: E/AndroidRuntime(1109): at android.app.ActivityThread.main(ActivityThread.java:3701) 02-07 00:17:17.827: E/AndroidRuntime(1109): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 00:17:17.827: E/AndroidRuntime(1109): at java.lang.reflect.Method.invoke(Method.java:507) 02-07 00:17:17.827: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 02-07 

00:17:17.827: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 02-07 00:17:17.827: 

E/AndroidRuntime(1109): at dalvik.system.NativeStart.main(Native Method)

我的应用首先使用open()打开数据库。

MyDB.java

public SQLiteDatabase db=null; 
private Context mCtx = null;
public MyDB(Context ctx){  
    this.mCtx = ctx;      
}
public void open() throws SQLException { // 開啟已經存在的資料庫  
    db = mCtx.openOrCreateDatabase(DATABASE_NAME, 0, null);
        try {       
          db.execSQL(CREATE_TABLE);// 建立資料表 
      }catch (Exception e) {    
      } 
  }

2 个答案:

答案 0 :(得分:0)

使用db.insert方法并将值存储到ContentaValues objest ..

请参阅this blog以获取SQLiteDatabase示例

答案 1 :(得分:0)

private static final String INSERT = "insert into contact(name,number) values(?,?)";
public DataHelper(Context context)
    {
        this.context=context;
        openhelper=new OpenHelper(context);
        this.db=openhelper.getWritableDatabase();
        this.insertStmt1 = this.db.compileStatement(INSERT);

    }

public boolean insert(String name, String number)
    {
    openhelper=new OpenHelper(context);
    db=openhelper.getWritableDatabase();

    String person=name;


    String values1=number;

    this.insertStmt1.bindString(1, person);
    this.insertStmt1.bindString(2, number);

    this.insertStmt1.executeInsert();

   Toast.makeText(context, "Value inserted", 10000).show();
    openhelper.Close_DB(db); 
    return true;
    }

这是一个将值插入数据库的示例,根据您的需要对其进行修改。 :)