我试图将数据插入表Facture中,但是我收到了一段代码的崩溃报告:“ NOT NULL约束失败:Facture.Mode(代码1299)”。 我没有找到修复它的方法。有人可以帮我吗?
有我的logcat。
07-22 13:59:01.565 2471-2471/com.example.pc.myapplication E/SQLiteDatabase: Error inserting U4=100 U1=100 A4=aa Q5=5 Q4=null A3=aaa A5=aa P5=100 P3=100 P4=100 U2=100 Q1=1 P2=100 A1=aa Q2=2 U5=100 Fournisseur=test Q3=3 U3=100 Datefactu=2/07/2018 Numero=123 A2=aaa Mode=null P1=100
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: Facture.Mode (code 1299)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.pc.myapplication.DatabaseHelper.insertFacture(DatabaseHelper.java:178)
at com.example.pc.myapplication.facture1.onOKClick(facture1.java:103)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
这是我的class facture.java:
public void onOKClick ( View v )
{
if (v.getId() == R.id.buttonfacok) {
EditText numero = (EditText)findViewById(R.id.Etnum);
EditText date = (EditText)findViewById(R.id.Etdat);
EditText fournisseur = (EditText)findViewById(R.id.TVfour);
EditText modepaiement = (EditText)findViewById(R.id.TVmode);
EditText article1 = (EditText)findViewById(R.id.A1);
EditText article2 = (EditText)findViewById(R.id.A2);
EditText article3 = (EditText)findViewById(R.id.A3);
EditText article4 = (EditText)findViewById(R.id.A4);
EditText article5 = (EditText)findViewById(R.id.A5);
EditText quantite1 = (EditText)findViewById(R.id.Q1);
EditText quantite2 = (EditText)findViewById(R.id.Q2);
EditText quantite3 = (EditText)findViewById(R.id.Q3);
EditText quantite4 = (EditText)findViewById(R.id.Q4);
EditText quantite5 = (EditText)findViewById(R.id.Q5);
EditText unite1 = (EditText)findViewById(R.id.U1);
EditText unite2 = (EditText)findViewById(R.id.U2);
EditText unite3 = (EditText)findViewById(R.id.U3);
EditText unite4 = (EditText)findViewById(R.id.U4);
EditText unite5 = (EditText)findViewById(R.id.U5);
EditText prix1 = (EditText)findViewById(R.id.P1);
EditText prix2 = (EditText)findViewById(R.id.P2);
EditText prix3 = (EditText)findViewById(R.id.P3);
EditText prix4 = (EditText)findViewById(R.id.P4);
EditText prix5 = (EditText)findViewById(R.id.P5);
String strnumero = numero.getText().toString();
String strdate = date.getText().toString();
String strfournisseur = fournisseur.getText().toString();
String strmodepaiement = modepaiement.getText().toString();
String strarticle1 = article1.getText().toString();
String strarticle2 = article2.getText().toString();
String strarticle3 = article3.getText().toString();
String strarticle4 = article4.getText().toString();
String strarticle5 = article5.getText().toString();
String strquantite1 = quantite1.getText().toString();
String strquantite2 = quantite2.getText().toString();
String strquantite3 = quantite3.getText().toString();
String strquantite4 = quantite4.getText().toString();
String strquantite5 = quantite5.getText().toString();
String strunite1 = unite1.getText().toString();
String strunite2 = unite2.getText().toString();
String strunite3 = unite3.getText().toString();
String strunite4 = unite4.getText().toString();
String strunite5 = unite5.getText().toString();
String strprix1 = prix1.getText().toString();
String strprix2 = prix2.getText().toString();
String strprix3 = prix3.getText().toString();
String strprix4 = prix4.getText().toString();
String strprix5 = prix5.getText().toString();
Facture f = new Facture();
f.setNumero(strnumero);
f.setDatefactu(strdate);
f.setFournisseur(strfournisseur);
f.setMode(strmodepaiement);
f.setA1(strarticle1);
f.setA2(strarticle2);
f.setA3(strarticle3);
f.setA4(strarticle4);
f.setA5(strarticle5);
f.setQ1(strquantite1);
f.setQ2(strquantite2);
f.setQ3(strquantite3);
f.setQ4(strquantite4);
f.setQ5(strquantite5);
f.setU1(strunite1);
f.setU2(strunite2);
f.setU3(strunite3);
f.setU4(strunite4);
f.setU5(strunite5);
f.setP1(strprix1);
f.setP2(strprix2);
f.setP3(strprix3);
f.setP4(strprix4);
f.setP5(strprix5);
helper.insertFacture(f);
}
}
}
有我的数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4 ;
private static final String DATABASE_NAME = "contacts.db" ;
private static final String TABlE_NAME = "contacts" ;
private static final String COLUMN_ID = "id" ;
private static final String COLUMN_NOMETPRENOM = "nometprenom" ;
private static final String COLUMN_CIN = "cin" ;
private static final String COLUMN_MOTDEPASSE = "motdepasse" ;
private static final String TABlE_NAME1 = "Argent" ;
private static final String COLUMN_ENTREE = "Entree" ;
private static final String COLUMN_DATE = "date" ;
private static final String TABlE_NAME2 = "Facture" ;
private static final String COLUMN_NUMERO = "Numero" ;
private static final String COLUMN_DATEFOU = "Datefactu" ;
private static final String COLUMN_FOURNISSEUR = "Fournisseur" ;
private static final String COLUMN_MODE = "Mode" ;
private static final String COLUMN_Article1 = "A1" ;
private static final String COLUMN_Article2 = "A2" ;
private static final String COLUMN_Article3 = "A3" ;
private static final String COLUMN_Article4 = "A4" ;
private static final String COLUMN_Article5 = "A5" ;
private static final String COLUMN_Quantite1 = "Q1" ;
private static final String COLUMN_Quantite2 = "Q2" ;
private static final String COLUMN_Quantite3 = "Q3" ;
private static final String COLUMN_Quantite4 = "Q4" ;
private static final String COLUMN_Quantite5= "Q5" ;
private static final String COLUMN_Unitaire1 = "U1" ;
private static final String COLUMN_Unitaire2 = "U2" ;
private static final String COLUMN_Unitaire3 = "U3" ;
private static final String COLUMN_Unitaire4 = "U4" ;
private static final String COLUMN_Unitaire5 = "U5" ;
private static final String COLUMN_Prix1 = "P1" ;
private static final String COLUMN_Prix2 = "P2" ;
private static final String COLUMN_Prix3 = "P3" ;
private static final String COLUMN_Prix4 = "P4" ;
private static final String COLUMN_Prix5 = "P5" ;
SQLiteDatabase db;
private static final String TABlE_CREATE = "create table contacts ( id integer primary key not null , nometprenom Text not null , cin Text not null , motdepasse Text not null);" ;
private static final String TABlE_CREATE1 = "create table Argent ( id integer primary key not null , date Text not null , Entree Text not null);" ;
private static final String TABlE_CREATE2 = "create table Facture ( id integer primary key not null , Datefactu Text not null , Numero Text not null , Fournisseur Text not null , Mode Text not null , A1 Text not null , A2 Text not null , A3 Text not null , A4 Text not null , A5 Text not null , Q1 Text not null , Q2 Text not null , Q3 Text not null , Q4 Text not null , Q5 Text not null , U1 Text not null , U2 Text not null , U3 Text not null , U4 Text not null , U5 Text not null , P1 Text not null , P2 Text not null , P3 Text not null , P4 Text not null , P5 Text not null);" ;
public DatabaseHelper(Context context)
{
super(context ,DATABASE_NAME , null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABlE_CREATE2);
this.db=db;
}
public void insertFacture(Facture f)
{
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Facture" ;
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
values.put(COLUMN_NUMERO, f.getNumero());
values.put(COLUMN_DATEFOU , f.getDatefactu());
values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
values.put(COLUMN_MODE, f.getMode());
values.put(COLUMN_Article1, f.getA1());
values.put(COLUMN_Article2, f.getA2());
values.put(COLUMN_Article3, f.getA3());
values.put(COLUMN_Article4, f.getA4());
values.put(COLUMN_Article5, f.getA5());
values.put(COLUMN_Quantite1, f.getQ1());
values.put(COLUMN_Quantite2, f.getQ2());
values.put(COLUMN_Quantite3, f.getQ3());
values.put(COLUMN_Quantite4, f.getQ4());
values.put(COLUMN_Quantite5, f.getQ5());
values.put(COLUMN_Unitaire1, f.getU1());
values.put(COLUMN_Unitaire2, f.getU2());
values.put(COLUMN_Unitaire3, f.getU3());
values.put(COLUMN_Unitaire4, f.getU4());
values.put(COLUMN_Unitaire5, f.getU5());
values.put(COLUMN_Prix1, f.getP1());
values.put(COLUMN_Prix2, f.getP2());
values.put(COLUMN_Prix3, f.getP3());
values.put(COLUMN_Prix4, f.getP4());
values.put(COLUMN_Prix5, f.getP5());
db.insert(TABlE_NAME2,null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query2 = "DROP TABLE IF EXISTS "+TABlE_NAME2 ;
db.execSQL(query2);
this.onCreate(db);
}
}
谢谢。
答案 0 :(得分:0)
根据Error inserting U4=100 U1=100 A4=aa Q5=5 Q4=null A3=aaa A5=aa P5=100 P3=100 P4=100 U2=100 Q1=1 P2=100 A1=aa Q2=2 U5=100 Fournisseur=test Q3=3 U3=100 Datefactu=2/07/2018 Numero=123 A2=aaa Mode=null P1=100
为空,您已经定义了表,因此根据.... , Mode Text not null , ....
不能为空。
您需要将Facture对象传递给提供或导致使用非空值的insertFacture方法(如果Mode不为null,则只能设置列的ContentValue),或者需要从列的定义中删除not null约束。
评论
@crammeur“模式”不能为null。我声明为字符串,但没有结果
声明变量时,其值将为null(除非它是原始类型)。
一种解决方法是将以下内容用作insertFacture
方法:-
public void insertFacture(Facture f) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from Facture" ;
Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();
values.put(COLUMN_NUMERO, f.getNumero());
values.put(COLUMN_DATEFOU , f.getDatefactu());
values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
if (f.getMode != null ) {
values.put(COLUMN_MODE, f.getMode());
} else {
values.put(COLUMN_MODE,"No mode supplied");
}
values.put(COLUMN_Article1, f.getA1());
values.put(COLUMN_Article2, f.getA2());
values.put(COLUMN_Article3, f.getA3());
values.put(COLUMN_Article4, f.getA4());
values.put(COLUMN_Article5, f.getA5());
values.put(COLUMN_Quantite1, f.getQ1());
values.put(COLUMN_Quantite2, f.getQ2());
values.put(COLUMN_Quantite3, f.getQ3());
values.put(COLUMN_Quantite4, f.getQ4());
values.put(COLUMN_Quantite5, f.getQ5());
values.put(COLUMN_Unitaire1, f.getU1());
values.put(COLUMN_Unitaire2, f.getU2());
values.put(COLUMN_Unitaire3, f.getU3());
values.put(COLUMN_Unitaire4, f.getU4());
values.put(COLUMN_Unitaire5, f.getU5());
values.put(COLUMN_Prix1, f.getP1());
values.put(COLUMN_Prix2, f.getP2());
values.put(COLUMN_Prix3, f.getP3());
values.put(COLUMN_Prix4, f.getP4());
values.put(COLUMN_Prix5, f.getP5());
db.insert(TABlE_NAME2,null, values);
}
如果getMode
方法的值返回null,则它将使用未提供模式的值。虽然不一定是理想的解决方案。真正的解决方法是确定为什么f.setMode(strmodepaiement);
返回null,这很可能是由于Facture的setMode
方法将Mode设置为null(或者根本没有设置Mode)所致。