当前位置: 首页 > 知识库问答 >
问题:

在Android中单击键盘的“完成”按钮时,将数据插入数据库中

国斌斌
2023-03-14

我想知道在将数据插入数据库时按下键盘的“完成”按钮时,我该怎么办?

我在这里使用多个编辑文本,当遇到最后一个编辑按钮时,我必须将所有数据插入数据库。

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

共有1个答案

芮学
2023-03-14

做一个

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

< br >现在调用< code>DatabaseHelper类的< code>insert方法

dbHelper.insertRecord(....参数...);

你写了祝酒词的地方

 类似资料:
  • 我试图更新下载按钮点击数据库列。似乎很容易,但当我点击按钮数据库中的计数器不更新。 我不确定这是否是最佳和正确的方法,但这是我到目前为止所做的。 一个输入字段,用户从API中提取图像 当图像加载有显示按钮下载和用户可以下载它 另外,当用户搜索图像时,我会将搜索的内容保存在数据库中。这是我的控制器函数,用于将图像保存在数据库中的搜索字符串中。这很好用 这是下载时更新按钮单击的功能 视图中的按钮 路线

  • 本文向大家介绍Android 将数据插入数据库,包括了Android 将数据插入数据库的使用技巧和注意事项,需要的朋友参考一下 示例            

  • 系统自带数字键盘,添加“完成”自定义按钮。 作者说:今天临时急用数字键盘,发现系统的没带“完成”按钮。在这上面搜的都是人家写的自定义数字键盘太麻烦。所以在系统自带的数字键盘,加了个自定义按钮。支持各个版本,里面加了iphone 5的4寸屏幕判断。 [Code4App.com]

  • 我面临一些问题,你可以很容易地解决这个问题。我使其在第一次查看时工作良好的表单,因为它在单击提交按钮时将数据存储在数据库中。我还可以在刷新配置文件页面后存储数据。我认为如果代码只在点击提交按钮时执行,而不在刷新页面时执行,那么问题就解决了。但我不知道怎么做。 轮廓php Frnd.php 编辑:我使用POST/Redirect/GET方法解决了这个问题。这个视频(https://www.youtu

  • 我想我在这段代码中遇到了一点问题:当我试图在数据库中插入值时,我遇到了一个错误。

  • 我忙于一项学校作业,我有一个由三个表组成的基本access数据库。Book、Reader和BookRated,其中as BookRated是交集表,主键userName来自Reader和ISBN来自Book组成BookRated的复合主键(作为外键)。 我正试图将信息插入书签表中,但出现以下错误