Java 从数据库检索数据并放入textview
我成功地将数据插入到数据库中,但当我尝试检索数据并在textView中显示时,它会被定向到MainActivity Regiser_page.java如下所示Java 从数据库检索数据并放入textview,java,android,android-studio,Java,Android,Android Studio,我成功地将数据插入到数据库中,但当我尝试检索数据并在textView中显示时,它会被定向到MainActivity Regiser_page.java如下所示 public class Register_Page extends Activity { int from_Where_I_Am_Coming = 0; private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb ; Button b1,b2; Te
public class Register_Page extends Activity {
int from_Where_I_Am_Coming = 0;
private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb ;
Button b1,b2;
TextView name ;
TextView email;
TextView pass;
TextView dob;
TextView gender;
TextView phone;
TextView city;
int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register__page);
name = (TextView) findViewById(R.id.editText_name);
phone = (TextView) findViewById(R.id.editText_phone);
email = (TextView) findViewById(R.id.editText_email);
pass = (TextView) findViewById(R.id.editText_pass);
dob = (TextView) findViewById(R.id.editText_dob);
gender=(TextView)findViewById(R.id.editText_gender);
city=(TextView)findViewById(R.id.editText_city);
b1=(Button)findViewById(R.id.button_register);
b2=(Button)findViewById(R.id.button_view);
mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this);
public void addUser(View view) {
if(mydb.insertContact(name.getText().toString(), email.getText().toString(),
pass.getText().toString(), dob.getText().toString(),
gender.getText().toString(),phone.getText().toString(),city.getText().toString())){
Toast.makeText(getApplicationContext(), "done",
Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "not done",
Toast.LENGTH_SHORT).show();
}
}
}
public void getmydata(View v){
Intent intent =new Intent();
intent.setClass(this ,View_Profile.class);
startActivity(intent);
}
Activity_register_page.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="no.nordicsemi.android.nrftoolbox.Register_Page">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/editText_name"
android:layout_alignParentTop="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="28dp"
android:layout_weight="0.36"
android:text="Register Here"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#3333ff"
android:textSize="28dp" />
<Button
android:id="@+id/button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="790dp"
android:onClick="mydata"
android:text="View data" />
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="70sp"
android:autoText="false"
android:text="Name :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="100sp"
android:ems="10"
android:hint="Enter your name"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="160sp"
android:text="Email-id :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="190sp"
android:ems="10"
android:hint="Enter Email-id"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="250dp"
android:hint="Enter Password"
android:text="Password"
android:textColor="#3368ff"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="280dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView_dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="450sp"
android:text="Date of Birth :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="480dp"
android:ems="10"
android:hint="Date of Birth"
android:inputType="date" />
<TextView
android:id="@+id/textView_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="540sp"
android:text="Phone No. :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="570sp"
android:ems="10"
android:hint="Enter Phone No."
android:inputType="phone" />
<TextView
android:id="@+id/textView_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="630sp"
android:text="City :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="660sp"
android:ems="10"
android:hint="Enter your City"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="340sp"
android:text="Gender :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="370dp"
android:ems="10"
android:hint="M/F/Others"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_register"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="720dp"
android:background="#3368ff"
android:onClick="addUser"
android:text="Register"
android:textAlignment="center"
android:textColor="#ffffff" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
public class View_Profile extends AppCompatActivity {
TextView name, email, pass, phone, gender, city, dob;
int id_To_Update=0;
private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view__profile);
name = (TextView) findViewById(R.id.textView_name);
phone = (TextView) findViewById(R.id.textView_phone);
email = (TextView) findViewById(R.id.textView_email);
pass = (TextView) findViewById(R.id.textView_pass);
gender = (TextView) findViewById(R.id.textView_gender);
dob = (TextView) findViewById(R.id.textView_dob);
city = (TextView) findViewById(R.id.textView_city);
mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this);
Cursor rs = mydb.getData(1);
id_To_Update = 1;
rs.moveToFirst();
String nam = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_NAME));
String phon = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_PHONE));
String emai = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_EMAIL));
String cit = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_CITY));
String gend = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_GENDER));
String pas = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_PASS));
String dobb = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_DOB));
if (!rs.isClosed()) {
rs.close();
}
name.setText((CharSequence)nam);
name.setFocusable(false);
name.setClickable(false);
email.setText((CharSequence)emai);
email.setFocusable(false);
email.setClickable(false);
pass.setText((CharSequence)pas);
pass.setFocusable(false);
pass.setClickable(false);
dob.setText((CharSequence)dobb);
dob.setFocusable(false);
dob.setClickable(false);
gender.setText((CharSequence)gend);
gender.setFocusable(false);
gender.setClickable(false);
phone.setText((CharSequence)phon);
phone.setFocusable(false);
phone.setClickable(false);
city.setText((CharSequence)cit);
city.setFocusable(false);
city.setClickable(false);
}
}
public class myDbAdapter extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_EMAIL = "email";
public static final String CONTACTS_COLUMN_PASS = "password";
public static final String CONTACTS_COLUMN_DOB = "date-of-birth";
public static final String CONTACTS_COLUMN_GENDER = "gender";
public static final String CONTACTS_COLUMN_PHONE="phone";
public static final String CONTACTS_COLUMN_CITY="city";
private HashMap hp;
public myDbAdapter(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, name text,email text,pass text, dob text,gender text,phone text,city text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String name, String email, String pass, String dob,String gender,String phone,String city) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("email", email);
contentValues.put("password", pass);
contentValues.put("date-of-birth", dob);
contentValues.put("gender", gender);
contentValues.put("phone",phone);
contentValues.put("city",city);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String email, String pass, String dob,String gender,String phone,String city) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("email", email);
contentValues.put("password", pass);
contentValues.put("date-of-birth", dob);
contentValues.put("gender", gender);
contentValues.put("phone",phone);
contentValues.put("city",city);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts() {
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
}
myDbAdapter.java如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="no.nordicsemi.android.nrftoolbox.Register_Page">
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/editText_name"
android:layout_alignParentTop="true"
android:layout_marginLeft="100dp"
android:layout_marginTop="28dp"
android:layout_weight="0.36"
android:text="Register Here"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#3333ff"
android:textSize="28dp" />
<Button
android:id="@+id/button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="790dp"
android:onClick="mydata"
android:text="View data" />
<TextView
android:id="@+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="70sp"
android:autoText="false"
android:text="Name :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="100sp"
android:ems="10"
android:hint="Enter your name"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="160sp"
android:text="Email-id :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="190sp"
android:ems="10"
android:hint="Enter Email-id"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="250dp"
android:hint="Enter Password"
android:text="Password"
android:textColor="#3368ff"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="280dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView_dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="450sp"
android:text="Date of Birth :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="480dp"
android:ems="10"
android:hint="Date of Birth"
android:inputType="date" />
<TextView
android:id="@+id/textView_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="540sp"
android:text="Phone No. :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="570sp"
android:ems="10"
android:hint="Enter Phone No."
android:inputType="phone" />
<TextView
android:id="@+id/textView_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="630sp"
android:text="City :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="660sp"
android:ems="10"
android:hint="Enter your City"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14sp"
android:layout_marginTop="340sp"
android:text="Gender :"
android:textColor="#3368ff"
android:textColorLink="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="370dp"
android:ems="10"
android:hint="M/F/Others"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_register"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="720dp"
android:background="#3368ff"
android:onClick="addUser"
android:text="Register"
android:textAlignment="center"
android:textColor="#ffffff" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
public class View_Profile extends AppCompatActivity {
TextView name, email, pass, phone, gender, city, dob;
int id_To_Update=0;
private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view__profile);
name = (TextView) findViewById(R.id.textView_name);
phone = (TextView) findViewById(R.id.textView_phone);
email = (TextView) findViewById(R.id.textView_email);
pass = (TextView) findViewById(R.id.textView_pass);
gender = (TextView) findViewById(R.id.textView_gender);
dob = (TextView) findViewById(R.id.textView_dob);
city = (TextView) findViewById(R.id.textView_city);
mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this);
Cursor rs = mydb.getData(1);
id_To_Update = 1;
rs.moveToFirst();
String nam = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_NAME));
String phon = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_PHONE));
String emai = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_EMAIL));
String cit = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_CITY));
String gend = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_GENDER));
String pas = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_PASS));
String dobb = rs.getString(rs.getColumnIndex(mydb.CONTACTS_COLUMN_DOB));
if (!rs.isClosed()) {
rs.close();
}
name.setText((CharSequence)nam);
name.setFocusable(false);
name.setClickable(false);
email.setText((CharSequence)emai);
email.setFocusable(false);
email.setClickable(false);
pass.setText((CharSequence)pas);
pass.setFocusable(false);
pass.setClickable(false);
dob.setText((CharSequence)dobb);
dob.setFocusable(false);
dob.setClickable(false);
gender.setText((CharSequence)gend);
gender.setFocusable(false);
gender.setClickable(false);
phone.setText((CharSequence)phon);
phone.setFocusable(false);
phone.setClickable(false);
city.setText((CharSequence)cit);
city.setFocusable(false);
city.setClickable(false);
}
}
public class myDbAdapter extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
public static final String CONTACTS_COLUMN_EMAIL = "email";
public static final String CONTACTS_COLUMN_PASS = "password";
public static final String CONTACTS_COLUMN_DOB = "date-of-birth";
public static final String CONTACTS_COLUMN_GENDER = "gender";
public static final String CONTACTS_COLUMN_PHONE="phone";
public static final String CONTACTS_COLUMN_CITY="city";
private HashMap hp;
public myDbAdapter(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, name text,email text,pass text, dob text,gender text,phone text,city text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String name, String email, String pass, String dob,String gender,String phone,String city) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("email", email);
contentValues.put("password", pass);
contentValues.put("date-of-birth", dob);
contentValues.put("gender", gender);
contentValues.put("phone",phone);
contentValues.put("city",city);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String email, String pass, String dob,String gender,String phone,String city) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("email", email);
contentValues.put("password", pass);
contentValues.put("date-of-birth", dob);
contentValues.put("gender", gender);
contentValues.put("phone",phone);
contentValues.put("city",city);
db.update("contacts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts() {
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
}
公共类myDbAdapter扩展了SQLiteOpenHelper{
公共静态最终字符串数据库\u NAME=“MyDBName.db”;
公共静态最终字符串CONTACTS\u TABLE\u NAME=“CONTACTS”;
公共静态最终字符串联系人\u COLUMN\u ID=“ID”;
公共静态最终字符串CONTACTS\u COLUMN\u NAME=“NAME”;
public static final String CONTACTS\u COLUMN\u EMAIL=“EMAIL”;
public static final String CONTACTS\u COLUMN\u PASS=“password”;
public static final String CONTACTS_COLUMN_DOB=“出生日期”;
public static final String CONTACTS\u COLUMN\u GENDER=“GENDER”;
public static final String CONTACTS\u COLUMN\u PHONE=“PHONE”;
public static final String CONTACTS\u COLUMN\u CITY=“CITY”;
私有HashMap-hp;
公共myDbAdapter(上下文){
super(上下文,数据库名称,null,1);
}
@凌驾
public void onCreate(SQLiteDatabase db){
//TODO自动生成的方法存根
db.execSQL(
“创建表联系人”+
(id整数主键、姓名文本、电子邮件文本、密码文本、dob文本、性别文本、电话文本、城市文本)
);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//TODO自动生成的方法存根
db.execSQL(“如果存在联系人,则删除表”);
onCreate(db);
}
公共布尔值insertContact(字符串名称、字符串电子邮件、字符串通行证、字符串dob、字符串性别、字符串电话、字符串城市){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
contentValues.put(“name”,name);
contentValues.put(“电子邮件”,email);
contentValues.put(“密码”,pass);
contentValues.put(“出生日期”,dob);
contentValues.put(“性别”,性别);
contentValues.put(“phone”,phone);
contentValues.put(“城市”,城市);
db.insert(“contacts”,null,contentValues);
返回true;
}
公共游标getData(int-id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery(“从联系人中选择*,其中id=“+id+”,null);
返回res;
}
公共int numberOfRows(){
SQLiteDatabase db=this.getReadableDatabase();
int numRows=(int)DatabaseUtils.queryNumEntries(db,CONTACTS\u TABLE\u NAME);
返回numRows;
}
public boolean updateContact(整数id、字符串名称、字符串电子邮件、字符串密码、字符串dob、字符串性别、字符串电话、字符串城市){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
contentValues.put(“name”,name);
contentValues.put(“电子邮件”,email);
contentValues.put(“密码”,pass);
contentValues.put(“出生日期”,dob);
contentValues.put(“性别”,性别);
contentValues.put(“phone”,phone);
contentValues.put(“城市”,城市);
update(“contacts”,contentValues,“id=?”,新字符串[]{Integer.toString(id)});
返回true;
}
公共整数删除联系人(整数id){
SQLiteDatabase db=this.getWritableDatabase();
返回db.delete(“联系人”,
“id=?”,
新字符串[]{Integer.toString(id)});
}
公共阵列列表getAllCotacts(){
ArrayList数组_list=新建ArrayList();
//hp=新HashMap();
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery(“从联系人中选择*”,null);
res.moveToFirst();
while(res.isAfterLast()==false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
res.moveToNext();
}
返回数组_列表;
}
}
将View_Profile定向到MainActivity并导致结果显示失败的错误是什么
我的日志文件显示此错误:
near "-": syntax error
Error inserting phone=5895346896 email=man@gmail.com name=my name gender=male password=47ufvjtio city=bangalore date-of-birth=30-02-95
android.database.sqlite.SQLiteException: near "-": syntax error (code 1): , while compiling: INSERT INTO contacts(phone,email,name,gender,password,city,date-of-birth) VALUES (?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at no.nordicsemi.android.nrftoolbox.myDbAdapter.insertContact(myDbAdapter.java:60)
at no.nordicsemi.android.nrftoolbox.Register_Page.addUser(Register_Page.java:194)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19888)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5276)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
near“-”:语法错误
插入电话时出错=5895346896电子邮件=man@gmail.com姓名=我的姓名性别=男性密码=47ufvjtio城市=班加罗尔出生日期=30-02-95
android.database.sqlite.SQLiteException:near“-”:语法错误(代码1),编译时:在联系人(电话、电子邮件、姓名、性别、密码、城市、出生日期)中插入值(?,,,,,,,,,?)
位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
位于android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
位于android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
位于android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
位于android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
位于android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
位于android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
在android.database.sqlite.SQLiteDatabase.insert(SQ
// public static final String CONTACTS_COLUMN_DOB = "date-of-birth";
// as
public static final String CONTACTS_COLUMN_DOB = "dob";