NHTorres
NHTorres

Reputation: 1538

exception insert SQLite android

I created this table

"CREATE TABLE IF NOT EXISTS Inscripcion (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
    hash TEXT, 
    codigo_evento TEXT, 
    ticket TEXT, 
    nombre TEXT, 
    inscripcion_id TEXT, 
    validado int, 
    sincronizado int, 
    numero TEXT, 
    asiento TEXT, 
    adicionales TEXT, 
    otros TEXT ,
    categoria TEXT, 
    codigo_usuario TEXT, 
    rut TEXT, 
    talla TEXT, 
    fecha_validacion TEXT,
    nombre_responsable TEXT
)";

I'm getting from my Web Service data to insert into my database and do it well:

String sql = "INSERT INTO 'Inscripcion' VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
SQLiteStatement statement = db.compileStatement(sql);
db.beginTransaction();
for (int p = 0; p < array.length(); p++) {
    statement.clearBindings();
    statement.bindString(1, rowParticipante.getString("hash"));
    statement.bindString(2, rowParticipante.getString("codigo_evento"));
    statement.bindString(3, rowParticipante.getString("ticket"));
    statement.bindString(4, rowParticipante.getString("nombre"));
    statement.bindString(5, rowParticipante.getString("inscripcion_id"));
    statement.bindString(6, rowParticipante.getString("validado"));
    statement.bindString(7, "0");
    statement.bindString(8, rowParticipante.getString("numero"));
    statement.bindString(9, rowParticipante.getString("categoria"));
    statement.bindString(10, rowParticipante.getString("asiento"));
    statement.bindString(11, rowParticipante.getString("otros"));
    statement.bindString(12, rowParticipante.getString("adicionales"));
    statement.bindString(13, codigoUser);
    statement.bindString(14, rowParticipante.getString("rut"));
    statement.bindString(15, rowParticipante.getString("talla"));
    statement.bindString(16, rowParticipante.getString("fecha_validacion"));
    statement.bindString(17, rowParticipante.getString("nombre_responsable"));
    statement.execute();
}
db.setTransactionSuccessful();  
db.endTransaction();

I guess this is fine, but I get the following error:

08-20 11:45:58.645: E/SQLiteLog(20145): (1) table Inscripcion has 18 columns but 17 values were supplied

I know it has 18 fields, but I have to add only 17, since the first, the id is auto incrementing. How could resolve this error?

Upvotes: 0

Views: 104

Answers (2)

Anton
Anton

Reputation: 471

Try to insert null as the first value. id will automatically increment and other 17 values will be written into the table. 18 total

Upvotes: -1

Marc B
Marc B

Reputation: 360802

In SQL INSERTs, if you don't specify which fields you're inserting your values into, you have to provide values for ALL of the fields.

e.g. in a simple table with fields (a,b,c):

INSERT INTO yourtable VALUES ('x', 'y', 'z'); // OK
INSERT INTO yourtable VALUES ('x', 'y'); // NOT ok

for the not ok version, how is the DB to know WHICH fields you want those values to go into? maybe it's a=>x, c=>y, or c=>x, b=>y

Your query should be

INSERT ... (field1, field2,... field17) VALUES (value1, value2, ... value17)

Upvotes: 2

Related Questions