如何存储数据

时间:2012-05-09 09:44:59

标签: android

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:无法将此sql的bindargs传递给newtable(name)值(?)

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);
        }
    }
}

任何人都可以帮助我

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

您的插入语句需要阅读:

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

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