Mam problem mały problem z SQLite.
Zacząłem robić poradnik ( a właściwie modyfikować go żeby zwyczajnie nie przepisywać 1:1 ) i napotkałem się na problem.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.contactapp, PID: 4320
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: contacts.id (code 1299)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:735)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1754)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1732)
at com.example.contactapp.MainActivity.addContact(MainActivity.java:136)
at com.example.contactapp.MainActivity.onClick(MainActivity.java:147)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
To mój kod:
Spoiler
package com.example.contactapp;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String DATABASE_NAME = "mycontacsdatabase";
EditText ETname, ETsurname, ETcity, ETstreet, EThomeNumber, EThouseNumber, ETphoneNumber, ETemail, ETdateOfBirth;
TextView TVlistContacts;
Spinner SPgroupContacts;
SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ETname = (EditText) findViewById(R.id.ETname);
ETsurname = (EditText) findViewById(R.id.ETsurname);
ETcity = (EditText) findViewById(R.id.ETcity);
ETstreet = (EditText) findViewById(R.id.ETstreet);
EThomeNumber = (EditText) findViewById(R.id.EThomeNumber);
EThouseNumber = (EditText) findViewById(R.id.EThouseNumber);
ETphoneNumber = (EditText) findViewById(R.id.ETphoneNumber);
ETemail = (EditText) findViewById(R.id.ETemail);
ETdateOfBirth = (EditText) findViewById(R.id.ETdateOfBirth);
TVlistContacts = (TextView) findViewById(R.id.TVlistContacts);
SPgroupContacts = (Spinner) findViewById(R.id.SPgroupContacts);
findViewById(R.id.BTNaddContact).setOnClickListener(this);
TVlistContacts.setOnClickListener(this);
mDatabase = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
createContactsTable();
}
private void createContactsTable() {
mDatabase.execSQL(
"CREATE TABLE IF NOT EXISTS contacts (\n" +
" id int NOT NULL CONSTRAINT contacts_pk PRIMARY KEY,\n" +
" namee varchar(200) NOT NULL,\n" +
" surnamee varchar(200) NOT NULL,\n" +
" cityy varchar(200) NOT NULL,\n" +
" streett varchar(200) NOT NULL,\n" +
" homeNumberr varchar(200) NOT NULL,\n" +
" houseNumberr varchar(200) NOT NULL,\n" +
" phoneNumberr varchar(200) NOT NULL,\n" +
" emaill varchar(200) NOT NULL,\n" +
" dateOfBirthh varchar(200) NOT NULL,\n" +
" groupp varchar(200) NOT NULL\n" +
");"
);
}
private boolean inputsAreCorrect(String namee, String surnamee, String cityy, String streett, String homeNumberr, String houseNumberr, String phoneNumberr, String emaill, String dateOfBirthh) {
if (namee.isEmpty()) {
ETname.setError("Wpisz imie");
ETname.requestFocus();
return false;
}
if (surnamee.isEmpty()) {
ETsurname.setError("Wpisz naziwsko");
ETsurname.requestFocus();
return false;
}
if (cityy.isEmpty()) {
ETcity.setError("Wpisz miasto");
ETcity.requestFocus();
return false;
}
if (streett.isEmpty()) {
ETstreet.setError("Wpisz ulice");
ETstreet.requestFocus();
return false;
}
if (homeNumberr.isEmpty()) {
EThomeNumber.setError("Wpisz numer domu");
EThomeNumber.requestFocus();
return false;
}
if (houseNumberr.isEmpty()) {
EThouseNumber.setError("Wpisz numer mieszkania");
EThomeNumber.requestFocus();
return false;
}
if (phoneNumberr.isEmpty()) {
ETphoneNumber.setError("Wpisz numer telefonu");
ETphoneNumber.requestFocus();
return false;
}
if (emaill.isEmpty()) {
ETemail.setError("Wpisz email");
ETemail.requestFocus();
return false;
}
if (dateOfBirthh.isEmpty()) {
ETdateOfBirth.setError("Wpisz date urodzenia");
ETdateOfBirth.requestFocus();
return false;
}
return true;
}
private void addContact() {
String namee = ETname.getText().toString().trim();
String surnamee = ETsurname.getText().toString().trim();
String cityy = ETcity.getText().toString().trim();
String streett = ETstreet.getText().toString().trim();
String homeNumberr = EThomeNumber.getText().toString().trim();
String houseNumberr = EThouseNumber.getText().toString().trim();
String phoneNumberr = ETphoneNumber.getText().toString().trim();
String emaill = ETemail.getText().toString().trim();
String dateOfBirthh = ETdateOfBirth.getText().toString().trim();
String groupp = SPgroupContacts.getSelectedItem().toString();
if (inputsAreCorrect(namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr, emaill, dateOfBirthh)) {
String insertSQL = "INSERT INTO contacts \n" +
"(namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr,emaill,dateOfBirthh, groupp)\n" +
"VALUES \n" +
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
mDatabase.execSQL(insertSQL, new String[]{namee, surnamee, cityy, streett, homeNumberr, houseNumberr, phoneNumberr, emaill, dateOfBirthh, groupp});
Toast.makeText(this, "Dodano kontakt!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.BTNaddContact:
addContact();
break;
case R.id.TVlistContacts:
startActivity(new Intent(this, ContactsActivity.class));
break;
}
}
}
Spoiler
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Dodaj nowy kontakt"
android:textAlignment="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textSize="30dp" />
<EditText
android:id="@+id/ETname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Imie"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETsurname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Nazwisko"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETcity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Miasto"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/ETstreet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ulica"
android:layout_marginRight="200dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/EThomeNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer domu"
android:inputType="number|textPersonName" />
<EditText
android:id="@+id/EThouseNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer mieszkania"
android:inputType="number|textPersonName" />
<EditText
android:id="@+id/ETphoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Numer telefonu"
android:inputType="textPersonName|phone" />
<EditText
android:id="@+id/ETemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailSubject" />
<EditText
android:id="@+id/ETdateOfBirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="200dp"
android:ems="10"
android:hint="Data urodzenia"
android:inputType="date" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wybierz grupę do której przypisać kontakt"
android:textAlignment="center"
android:layout_marginTop="20dp"
android:textSize="20dp" />
<Spinner
android:id="@+id/SPgroupContacts"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:entries="@array/groups"
android:layout_height="wrap_content" />
<Button
android:id="@+id/BTNaddContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodaj kontakt"
android:layout_marginTop="10dp"
android:textSize="20dp" />
<TextView
android:id="@+id/TVlistContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lista kontaktow"
android:textAlignment="center"
android:layout_marginTop="5dp"
android:textSize="18dp" />
</LinearLayout>
</ScrollView>
tutorial który wykonuje: https://www.simplifiedcoding.net/android-sqlite-database-example/
Czy ktoś może wskazać jak rozwiążę ten problem ?