我想知道在将数据插入数据库时按下键盘的DONE按钮应该怎么做?
我在这里使用多个edittext,当它到达最后一个编辑按钮时,我必须将所有数据插入到数据库中..
private static final String CREATE_DB_TABLE_BUSINESSINFO=
"CREATE TABLE " + TABLE_BUSINESS + "(" +
COL_BID + " INTEGER PRIMARY KEY AUTOINCREMENT,"+
COL_COMPANY_NAME + " TEXT NOT NULL ,"+
COL_ADD1 + " TEXT NOT NULL ," +
COL_ADD2 + " TEXT NOT NULL ," +
COL_ADD3 + " TEXT ,"+
COL_CITY +" TEXT NOT NULL ,"+
COL_PROVINCE + " TEXT NOT NULL ,"+
COL_POSTCODE + " TEXT NOT NULL ," +
COL_TEL1 + " TEXT NOT NULL ,"+
COL_TEL2 + " TEXT ,"+
COL_WEBSITE + " TEXT NOT NULL ,"+
COL_EMAIL + " TEXT NOT NULL ,"+
COL_TWITTER + " TEXT NOT NULL ,"+
COL_FACEBOOK + " TEXT NOT NULL ,"+
COL_WELBO + " TEXT NOT NULL" + ")" ;
private static final String CREATE_BUSINESS_RECORD = "INSERT INTO "
+ TABLE_BUSINESS + " (" + COL_COMPANY_NAME + ", "
+ COL_ADD1+","
+ COL_ADD2+","
+ COL_ADD3+","
+ COL_CITY+","
+ COL_PROVINCE+","
+ COL_POSTCODE+","
+ COL_TEL1+","
+ COL_TEL2+","
+ COL_WEBSITE+","
+ COL_EMAIL+","
+ COL_TWITTER+","
+ COL_FACEBOOK+","
+ COL_WELBO
+ ") VALUES (0,0,0,0,0,0,0,0,0,0,0,0,0,0);";
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE_BUSINESSINFO);
db.execSQL(CREATE_BUSINESS_RECORD);
}
public void inserRecord(EditText edtxt_company, EditText edtxt_add1,
EditText edtxt_add2, EditText edtxt_add3, EditText edtxt_city,
EditText edtxt_province, EditText edtxt_postcode,
EditText edtxt_tel1, EditText edtxt_tel2, EditText edtxt_website,
EditText edtxt_email, EditText edtxt_twitter,
EditText edtxt_facebook, EditText edtxt_welbo) {
/*SQLiteDatabase mdb1=mDbHelper.getWritableDatabase();*/
ContentValues record = new ContentValues();
record.put("companyname", edtxt_company.toString());
record.put("address1", edtxt_add1.toString());
record.put("address2", edtxt_add2.toString());
record.put("address3", edtxt_add3.toString());
record.put("city", edtxt_city.toString());
record.put("province", edtxt_province.toString());
record.put("postcode", edtxt_postcode.toString());
record.put("tel1", edtxt_tel1.toString());
record.put("tel2", edtxt_tel2.toString());
record.put("website", edtxt_website.toString());
record.put("email", edtxt_email.toString());
record.put("twitter", edtxt_twitter.toString());
record.put("facebook", edtxt_facebook.toString());
record.put("welbo", edtxt_welbo.toString());
mDb.insert(CREATE_DB_TABLE_BUSINESSINFO, null,record); }
IN MAINACTIVITY....FOR ADDING THE DATA.....
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.i("Insert Successfully","Insert");
addrow();
}
return false;
}
private void addrow() {
try
{
mDbHelper2.inserRecord(edtxt_company, edtxt_add1,edtxt_add2, edtxt_add3,edtxt_city, edtxt_province,edtxt_postcode,edtxt_tel1,edtxt_tel2,edtxt_website,edtxt_email, edtxt_twitter, edtxt_facebook, edtxt_welbo);
}
catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
制作一个
class DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper{
SQLiteDatabase dbms1;
DatabaseHelper dbHelper;
Cursor records;
public DatabaseHelper(Context context){
super(context,"UserDatabase", null,1);
Log.i("Information", "Database Created");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void createTable(){
try{
dbms1=getWritableDatabase();
String sql1="create table if not exists user(_id INTEGER PRIMARY KEY AUTOINCREMENT, name text not null,emailid text,gender text,username text,password text,birthdate text,phonenumber text,admin boolean,image BLOB);";
Log.i("haiyang:createDB=", sql1);
dbms1.execSQL(sql1);
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
}
public void closeDatabase() {
// TODO Auto-generated method stub
try{
dbms1.close();
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
}
public boolean inserRecord(String name,String emailid,String gender,String username,String password,String birthdate,String phonenumber,boolean admin,Drawable drawableResource) {
// TODO Auto-generated method stub
Bitmap image = ((BitmapDrawable)drawableResource).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, out);
ContentValues record=new ContentValues();
record.put("name",name);
record.put("emailid",emailid);
record.put("gender",gender);
record.put("username",username);
record.put("password",password);
record.put("birthdate",birthdate);
record.put("phonenumber",phonenumber);
record.put("admin",admin);
record.put("image",out.toByteArray());
try{
dbms1=this.getWritableDatabase();
dbms1.insert("user", null,record);
}
catch(SQLiteException e){
Log.i("SQLiteException",e.toString());
}
return true;
}
}
之后必须创建
的对象DatabaseHelper dbHelper=new DatabaseHelper(this);
dbHelper.createTable();
现在调用insert
DatabaseHelper class
方法
dbHelper.insertRecord(....arguments....);
你写过 Toast