gpt4 book ai didi

android - 如何存储数据

转载 作者:行者123 更新时间:2023-11-29 22:07:17 26 4
gpt4 key购买 nike

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 传递给新表(名称)值(?)

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

谁能帮帮我

最佳答案

改变;

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

关于android - 如何存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com