【问题标题】:Add Text From EditText to SQLite将 EditText 中的文本添加到 SQLite
【发布时间】:2011-06-26 02:21:10
【问题描述】:

我想这样当我单击按钮时,它会从EditText 获取文本并将其添加到SQLiteDatabase。我以前在某个地方读过它,但我忘了怎么做。

【问题讨论】:

    标签: android sqlite button android-edittext


    【解决方案1】:

    你必须看看以下函数:
    实现事件监听器:
    onClick()

    从 textedit 获取文本
    getText()

    向 SQLite 添加数据:
    insert()

    文档是最好的指南......

    【讨论】:

    • 实际上,它不会让我把 TABLE_NAME 作为我的字符串放在哪里。这是代码: ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { et.getText(); db.insert(TABLE_NAME, null, null); additem.dismiss(); et.setText( ""); } });`
    • 您之前创建过表吗? . insert() 还返回插入行的 rowid 。给它分配一个变量,看看它有什么......
    • stackoverflow.com/questions/754684/… :一篇很好的帖子,展示了如何将数据插入 SQlite。
    • 我在 DataHelper 类中创建了表,在此之前我有一个 SimpleCursorAdapter。
    • 其实我决定试试et.getText().toString()
    【解决方案2】:

    如何抓取文本,将其转换为字符串并将其放入查询中

    String TableName = "ComplicatedTableNameHere";  
    EditText editText1 = (EditText) findViewById(R.id.EditTextIDhere); 
    String editTextString1 = editText1.getText().toString();  
    

    崩溃

    String TableName = "ComplicatedTableNameHere";            
        //sets the table name as a string so you can refer to TableName instead of writing out your table name everytime
    
    EditText editText1 = (EditText) findViewById(R.id.EditTextIDhere); 
        //gets the text from your edit text fieldfield 
        //editText1 = your edit text name
        //EditTextIDhere = the id of your text field
    
    String editTextString1 = editText1.getText().toString();  
        //sets the edit text as a string
        //editText1 is the name of the Edit text from the (EditText) we defined above
        //editTextString1 = the string name you will refer to in future
    

    然后使用

           /* Insert data to a Table*/
           myDB.execSQL("INSERT INTO "
             + TableName
             + " (Column_Name, Column_Name2, Column_Name3, Column_Name4)"
             + " VALUES ( "+EditTextString1+", 'Column_Value2','Column_Value3','Column_Value4');");
    

    希望这对您有所帮助...

    【讨论】:

      【解决方案3】:

      你可以用这个:

      mydatebase.execSQL(
            "INSERT INTO xxx (name,number,age) VALUES ('"+YOUR_STRING_DATA+"',123456789,22)"
      );
      

      【讨论】:

      • 这是一种危险的方式,因为它可能导致SQL Injection。它也没有回答问题,即如何从 UI 组件中获取文本 - 请更新您的答案
      猜你喜欢
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 1970-01-01
      • 2021-01-06
      相关资源
      最近更新 更多