【问题标题】:Android SQLite database安卓 SQLite 数据库
【发布时间】:2016-12-11 20:53:48
【问题描述】:

我是一名初级安卓程序员。我有问题,无法解决。我想将钱包添加到数据库中,但应用程序给我一个错误。 似乎它采用了 EditText 的值。怎么了?

我有错误:

    Error inserting currency=null surname=null start_acc=0.0 name=null
                  android.database.sqlite.SQLiteConstraintException: NOT NULL

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dodawanie_portfela"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.xxx.xxx.xxx">

    <TextView
        android:text="xxx"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="29dp"
        android:layout_marginStart="29dp"
        android:layout_marginTop="75dp"
        android:id="@+id/textView3" />

    <TextView
        android:text="xxx"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignStart="@+id/textView3"
        android:layout_marginTop="20dp"
        android:id="@+id/textView4" />

    <TextView
        android:text="xxx"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4"
        android:layout_marginTop="20dp"
        android:id="@+id/textView5" />

    <TextView
        android:text="xxx"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5"
        android:layout_marginTop="20dp"
        android:id="@+id/textView6" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView6"
        android:layout_toRightOf="@+id/textView3"
        android:layout_toEndOf="@+id/textView3"
        android:layout_marginLeft="26dp"
        android:layout_marginStart="26dp"
        android:id="@+id/curenncy" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_above="@+id/textView4"
        android:layout_alignLeft="@+id/curenncy"
        android:layout_alignStart="@+id/curenncy"
        android:id="@+id/name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_above="@+id/textView5"
        android:layout_alignLeft="@+id/name"
        android:layout_alignStart="@+id/name"
        android:id="@+id/surname" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:layout_above="@+id/textView6"
        android:layout_alignLeft="@+id/curenncy"
        android:layout_alignStart="@+id/curenncy"
        android:id="@+id/cash" />

    <Button
        android:text="ADD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addwallet"
        android:width="500dp"
        android:background="@color/common_action_bar_splitter"
        android:elevation="0dp"
        android:layout_marginTop="78dp"
        android:layout_below="@+id/textView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

方法

 public void AddWallet(String name, String surname, int start_acc, int curenncy){
    ContentValues contentValues = new ContentValues();
    contentValues.put(Wallets.COLUMNS.name, wallet.getName());
    contentValues.put(Wallets.COLUMNS.surname, wallet.getSurname());
    contentValues.put(Wallets.COLUMNS.start_acc, wallet.getStart_acc());
    contentValues.put(Wallets.COLUMNS.curenncy, wallet.getCurrency());
    DbHelper.getWritableDatabase().insert(Wallets.NAME_TABLE,null,contentValues);
}

public class Add extends AppCompatActivity {
EditText name;
EditText surname;
EditText start_acc;
Button add;
Spinner curenncy;

int id_value;
DatabaseDEO db = new DatabaseDEO(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    name = (EditText) findViewById(R.id.name);
    surname = (EditText) findViewById(R.id.surname);
    start_acc = (EditText) findViewById(R.id.cash);
    add = (Button) findViewById(R.id.addwallet);
    curenncy = (Spinner) findViewById(R.id.curenncy);


    curenncy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


            id_value = position;

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


    final ArrayList<String> list= db.getToSpinner();
    ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,R.layout.currency_spinner, R.id.text,list);
    curenncy.setAdapter(adapter);



    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String pname = name.getText().toString();
            String psurname = surname.getText().toString();
            int pstart_acc = Integer.parseInt(start_acc.getText().toString());
            int pid_value = id_value;

            db.AddWallet(pname,psurname,pstart_acc,pid_value);

            Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_LONG).show();
        }
    });
}

【问题讨论】:

  • 您的一列不接受空值,但您从编辑文本中获得空值。确保您在edittext中输入了一些内容。您还应该在插入数据之前检查 null。
  • 是的,一列没有空属性,但我在edittext中写入文本并在微调器中选择选项。

标签: android sqlite null


【解决方案1】:

将上面的代码更改为此,它将起作用。我猜你是从一些模型类对象钱包中获取值,但你没有设置它们。

public void AddWallet(String name, String surname, int start_acc, int curenncy){
    ContentValues contentValues = new ContentValues();
    contentValues.put(Wallets.COLUMNS.name, name);
    contentValues.put(Wallets.COLUMNS.surname, surname);
    contentValues.put(Wallets.COLUMNS.start_acc, start_acc);
    contentValues.put(Wallets.COLUMNS.curenncy,curenncy);
    DbHelper.getWritableDatabase().insert(Wallets.NAME_TABLE,null,contentValues);
}

【讨论】:

  • 谢谢!工作!但我无法从 spinner 获得 ID 证书。也许你知道怎么做?必须调用curnency.setOnItemSelectedListener方法吗?
  • @Adrien 为什么要将微调器位置存储在数据库中?您应该在其中存储选定的项目值。你可以使用curnency.getSelectedItem().toString();如果你有整数值,你可以将其解析为 int。
  • 在数据库中,我有表 Curenncy,其中 id_curenncy 和 code_currency 列。 Spinner 值来自数据库(来自 code_currency 列的数据)。我想获取用户选择的 id_currency。
  • @Adrien 您应该尝试阅读此处的微调器文档以清楚地了解微调器。它不像你在制作它们。
  • 之前您写过关于缺少设定值的文章。我试着去做。但我有一个问题。你知道它应该是什么样子吗?
猜你喜欢
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多