在单击按钮时将数据插入 Android Studio 中的 SQlite 数据库
Posted
技术标签:
【中文标题】在单击按钮时将数据插入 Android Studio 中的 SQlite 数据库【英文标题】:Insert data into SQlite databases in Android Studio while button click 【发布时间】:2021-12-06 23:21:36 【问题描述】:我正在制作一个插入客户测量详细信息的应用程序。 但我的数据库有问题,我不知道这是什么问题。当我点击 addbtn 时,它显示 toast failed(数据未插入) 我尝试了所有方法,但没有任何效果。请帮助我解决为什么没有插入数据。
'''
package com.example.customermanagement;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Customer extends AppCompatActivity
EditText et1,et2,et3,et4,et5,et6,et7,et8,et9,et10;
Button addbtn;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__customer);
MyHelper myhelper=new MyHelper(this);
addbtn=(Button)findViewById(R.id.addbtn);
addbtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
et1 =(EditText)findViewById(R.id.name);
String customername=et1.getText().toString();
//showtxt=(EditText) findViewById(R.id.others);
//showtxt.setText(customername);
et2 =(EditText)findViewById(R.id.phone);
String phoneno=et2.getText().toString();
et3 =(EditText)findViewById(R.id.lenght);
String lenght=et3.getText().toString();
et4 =(EditText)findViewById(R.id.teera);
String teera=et4.getText().toString();
et5 =(EditText)findViewById(R.id.bazu);
String bazu=et5.getText().toString();
et6 =(EditText)findViewById(R.id.chati);
String chati=et6.getText().toString();
et7 =(EditText)findViewById(R.id.qamar);
String qamar=et7.getText().toString();
et8 =(EditText)findViewById(R.id.shalwar);
String shalwar=et8.getText().toString();
et9 =(EditText)findViewById(R.id.paincha);
String paincha=et9.getText().toString();
et10 =(EditText)findViewById(R.id.others);
String others=et10.getText().toString();
// Toast.makeText(Add_Customer.this,lenght,Toast.LENGTH_SHORT).show();
myhelper.getWritableDatabase();
Boolean i= myhelper.insert_data(customername,phoneno,lenght,teera,bazu,chati,qamar,shalwar,paincha,"7","7","7","7","7","7","9","9",others);
if(i==true)
Toast.makeText(Add_Customer.this,"succesfull enter",Toast.LENGTH_SHORT).show();
else
Toast.makeText(Add_Customer.this,"Fail",Toast.LENGTH_SHORT).show();
);
''' 还有我的数据库助手的代码
'''
package com.example.customermanagement;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.icu.text.StringPrepParseException;
import android.widget.Toast;
public class MyHelper extends SQLiteOpenHelper
private static final String dbname="mydb";
private static final int version=1;
public MyHelper(Context context)
super(context,dbname,null,version);
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
String sql="CREATE TABLE CUSTOMER ( _id INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT , PHONENO TEXT , LENGHT TEXT , TEERA TEXT , BAZU TEXT , CHATI TEXT , QAMAR TEXT , SHALWAR TEXT , PAINCHA TEXT , GHEERA TEXT , FRONT TEXT , SIDE TEXT , HIP TEXT , BAN TEXT , COLLAR TEXT , ZIP TEXT , T_PAINCHA TEXT , OTHERS TEXT )";
sqLiteDatabase.execSQL(sql);
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
sqLiteDatabase.execSQL("drop table if exists CUSTOMER");
onCreate(sqLiteDatabase);
public Boolean insert_data(String name1, String phoneno1,String lenght1,String teera1,String bazu1,String chati1,String qamar1,String shalwar1,String paincha1,String gheera1,String front1,String side1,String hip1,String ban1,String collar1,String zip1,String t_paincha1,String others1)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues c = new ContentValues();
long r = 0;
c.put("NAME", name1);
c.put("PHONENO", phoneno1);
c.put("LENGHT", lenght1);
c.put("TEERA", teera1);
c.put("BAZU", bazu1);
c.put("CHATI", chati1);
c.put("QAMAR", qamar1);
c.put("SHALWAR", shalwar1);
c.put("PAINCHA", paincha1);
c.put("GHEERA", gheera1);
c.put("FRONT", front1);
c.put("SIDE", side1);
c.put("HIP", hip1);
c.put("BAN", ban1);
c.put("COLLAR", collar1);
c.put("ZIP", zip1);
c.put("T_PAINCHA", t_paincha1);
c.put("OTHERS", others1);
r = db.insert("CUSTOMER", null, c);
if (r!=0)
return false;
else
return true;
'''
【问题讨论】:
db.insert
返回the row ID of the newly inserted row, or -1 if an error occurred
所以你的if (r != 0)
不正确。
【参考方案1】:
这不是对您帖子的回答。
有什么理由不使用 Google 的 Room 库?它非常容易/简单,并减少了所有与 sqlite 帮助器和游标相关的生物模板代码。对于我的个人项目,我总是使用 Room。
检查这个代码实验室:https://developer.android.com/codelabs/android-room-with-a-view-kotlin#0
【讨论】:
以上是关于在单击按钮时将数据插入 Android Studio 中的 SQlite 数据库的主要内容,如果未能解决你的问题,请参考以下文章
如何在c#中单击按钮时将datagridview值插入数据库