我已经尝试过创建一个注册数据库,仅用于知识目的。但是,我的数据库似乎无法捕获任何数据。我已经对我的代码进行了三次检查,但无济于事。任何帮助将不胜感激。
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contacts.db";
public static final String TABLE_NAME = "contacts";
public static final String COLUMN_ID = "id";
public static final String COL_1 = "Name";
public static final String COL_2 = "Pass";
public static final String COL_3 = "email";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
"name text not null, pass text not null, email text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertContact(Contact c)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from contacts";
Cursor cursor = db.rawQuery(query , null);
int count = cursor.getCount();
values.put(COLUMN_ID , count);
values.put(COL_1 , c.getName());
values.put(COL_2 , c.getPass());
values.put(COL_3 , c.getEmail());
db.insert(TABLE_NAME , null , values);
db.close();
}
public String searchPass(String name)
{
db = this.getReadableDatabase();
String query = "select Name, Pass from " + TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do {
a = cursor.getString(0);
if(a.equals(name))
{
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
return b;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}
Register.java
public class Register extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void onRegisterClick(View v)
{
if (v.getId()== R.id.button17){
EditText name = (EditText)findViewById(R.id.editText8);
EditText email = (EditText)findViewById(R.id.editText9);
EditText pass1 = (EditText)findViewById(R.id.editText10);
EditText pass2 = (EditText)findViewById(R.id.editText11);
String namestr = name.getText().toString();
String emailstr = email.getText().toString();
String pass1str = pass1.getText().toString();
String pass2str = pass2.getText().toString();
if (!pass1str.equals(pass2str))
{
Toast pass = Toast.makeText(Register.this , "Password don't match" , Toast.LENGTH_SHORT);
pass.show();
}
else
{
Contact c = new Contact();
c.setName(namestr);
c.setPass(pass1str);
c.setEmail(emailstr);
helper.insertContact(c);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_register, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Contacts.java
public class Contact {
String name,pass,email;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setPass(String pass)
{
this.pass = pass;
}
public String getPass()
{
return this.pass;
}
public void setEmail(String email)
{
this.email = email;
}
public String getEmail()
{
return this.email;
}
}
答案 0 :(得分:0)
您没有在数据库中插入正确的数据。您的方法应如下所示:
public void insertContact(Contact c)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_1 , c.getName());
values.put(COL_2 , c.getPass());
values.put(COL_3 , c.getEmail());
db.insert(TABLE_NAME , null , values);
db.close();
}
要编辑联系人,请添加以下方法:
public void updateContact(Contact c, int id)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_1 , c.getName());
values.put(COL_2 , c.getPass());
values.put(COL_3 , c.getEmail());
db.update(TABLE_NAME, values, "id = ? ", new String[]{Integer.toString(id)});
db.close();
}
其中" id"是您要更改它的项目的唯一ID。