我正在编写一个程序,该程序从用户那里获取插入的数据并将其插入到sql数据库中。
我编写了这段代码,将输入的数据插入数据库。但是无法插入数据。吐司豆说:“数据已成功保存”,但是我找不到数据。
这是mainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText edtId, edtName, edtFamily;
Button btnInsert, btnView, btnUpdate, btnDelete;
DatabaseManager dbm;
TextView txtCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtId = (EditText) findViewById(R.id.editId);
edtName = (EditText) findViewById(R.id.editName);
edtFamily = (EditText) findViewById(R.id.editFamily);
btnInsert = (Button) findViewById(R.id.buttonSave);
btnView = (Button) findViewById(R.id.buttonView);
btnUpdate = (Button) findViewById(R.id.buttonUpdate);
btnDelete = (Button) findViewById(R.id.buttonDelete);
dbm = new DatabaseManager(this);
txtCount = (TextView) findViewById(R.id.text_count);
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mId = edtId.getText().toString();
String mName = edtName.getText().toString();
String mFamily = edtFamily.getText().toString();
dbm.insertPerson(mId,mName,mFamily);
if (TextUtils.isEmpty(mId) || TextUtils.isEmpty(mName) || TextUtils.isEmpty(mFamily)) {
Toast.makeText(MainActivity.this,"complete all fields", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "data has been saved succesfully!", Toast.LENGTH_LONG).show();
}
}
});
这是数据库代码:
public class DatabaseManager extends SQLiteOpenHelper {
private static final String databasename="mydatabase.db";
private static final int databaseVersion=1;
private static final String tableName="mydatabase";
private static final String dId = "id";
private static final String dName ="name";
private static final String dFamiliy ="familiy";
public DatabaseManager( Context context) {
super(context, databasename, null, databaseVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
String cQuery = "CREATE TABLE mydatabase (id VARCHAR PRIMARY KEY, name VARCHAR, family VARCHAR)";
db.execSQL(cQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + tableName);
onCreate(db);
}
public boolean insertPerson (String pId,String pName,String pFamily){
SQLiteDatabase idb=this.getWritableDatabase();
ContentValues icv = new ContentValues();
icv.put(dId,pId);
icv.put(dName,pName);
icv.put(dFamiliy,pFamily);
long result= idb.insert(tableName,null,icv);
if (result==-1)
return false;
else
return true;
}
我该如何解决?