0

我尝试db_insert.javaMyDB.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);,应用程序会崩溃。

有人知道为什么吗?

日志猫

    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) {    
      } 
  }
4

2 回答 2

0

使用 db.insert 方法并将值存储到 ContentaValues 对象中。

请参阅此博客以获取 SQLiteDatabase 示例

于 2013-02-06T16:40:16.263 回答
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;
    }

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

于 2013-02-11T05:08:06.937 回答