1
public class SaveData extends Activity implements OnClickListener {  
    private DataManipulator dh;     
    static final int DIALOG_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addname);

        View add = findViewById(R.id.Button01add);
        add.setOnClickListener(this);
        View home = findViewById(R.id.Button01home);
        home.setOnClickListener(this);           
    }

    public void onClick(View v){
        switch(v.getId()){

        case R.id.Button01home:
            Intent i = new Intent(this, DatabaseSample.class);
            startActivity(i);
            break;

        case R.id.Button01add:
            View editText1 = (EditText) findViewById(R.id.name);
            String myEditText1=((TextView) editText1).getText().toString();

            this.dh = new DataManipulator(this);
            this.dh.insert(myEditText1);

            showDialog(DIALOG_ID);
            break;

        }
    }  
    protected final Dialog onCreateDialog(final int id) {
        Dialog dialog = null;
        switch(id) {
        case DIALOG_ID:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Information saved successfully ! Add Another Info?")
            .setCancelable(false)
            .setPositiveButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SaveData.this.finish();

              }
            })
            .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create(); 
            dialog = alert;
            break;

        default:

        }
        return dialog;
    }
}

当我单击模拟器上的保存数据时,我无法将数据保存到数据库中,它显示 java.lang.IllegalArgumentException: Can't pass bindargs for this sql :into newtable (name) values (?)

public class DataManipulator {
    private static final  String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;
    static final String TABLE_NAME = "newtable";
    private static Context context;
    static SQLiteDatabase db;

    private SQLiteStatement insertStmt;

    private static final String INSERT = " into "   + TABLE_NAME + " (name) values (?)";

    public DataManipulator(Context context) {
        DataManipulator.context = context;
        OpenHelper openHelper = new OpenHelper(DataManipulator.context);
        DataManipulator.db = openHelper.getWritableDatabase();
        this.insertStmt = DataManipulator.db.compileStatement(INSERT);

    }
    public long insert(String name) {
        this.insertStmt.bindString(1, name);
        return this.insertStmt.executeInsert();
    }

    public void deleteAll() {
        db.delete(TABLE_NAME, null, null);
    }

    public List<String[]> selectAll()
    {

        List<String[]> list = new ArrayList<String[]>();
        Cursor cursor = db.query(TABLE_NAME, new String[] { "name"},  null, null, null, null, null); 

        int x=0;
        if (cursor.moveToFirst()) {
            do {
                String[] b1=new String[]{cursor.getString(0)};

                list.add(b1);

                x=x+1;
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        } 
        cursor.close();

        return list;
    }


    public void delete(int rowId) {
        db.delete(TABLE_NAME, null, null); 
    }


    private static class OpenHelper extends SQLiteOpenHelper {

        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + " (name TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }
}

谁能帮我

4

2 回答 2

2

改变;

public long insert(String name) {
        this.insertStmt.bindString(1, name);
        return this.insertStmt.executeInsert();
}

到:

public long insert(String name) {
    ContentValues args = new ContentValues();
    args.put("name", name);
    return db.insert(TABLE_NAME, null, args);
}
于 2012-05-09T09:56:26.767 回答
0

您的插入语句需要阅读:

private static final String INSERT = "INSERT into "   + TABLE_NAME + " (name) values (?)";

编辑:Waqas 发布的方式是在 Android 中使用 SQLite 的更好方式。用他说的。

于 2012-05-09T09:57:34.053 回答