Problem with internal database

2

I'm trying to manipulate an internal database with the following code:

sql = "CREATE TABLE IF NOT EXISTS tipo ([codigo] integer autoincrement,nome text not null);"; 
bancoDados.execSQL(sql);
sql = "INSERT INTO \"tipo\" VALUES(1,\"ESTADUAL\");";
bancoDados.execSQL(sql);
sql = "INSERT INTO \"tipo\" VALUES(2,\"MUNICIPAL\");";
bancoDados.execSQL(sql);
sql = "INSERT INTO \"tipo\" VALUES(3,\"PRIVADA\");";
bancoDados.execSQL(sql);
sql = "INSERT INTO \"tipo\" VALUES(4,\"FEDERAL\");";

The error I'm having is the following:

03-23 20:22:26.419: E/Database(368): 
Failure 19 (PRIMARY KEY must be unique) on 0x92ba0 when executing 
'INSERT INTO "tipo" VALUES(1,"ESTADUAL");'

I've already tried a few things: to get the number that I was inserting myself (by being with an autoincrement) but did not give and continued the same error ... Does anyone know what's wrong?

    
asked by anonymous 23.03.2014 / 21:29

1 answer

1

Is your database empty? Its código field is auto increment, so if you have a single record that is then the 1 code will already be "busy". Ideally when dealing with fields of this type is to let the system itself assign the codes / IDs to you:

INSERT INTO "tipo"(nome) VALUES("ESTADUAL");
INSERT INTO "tipo"(nome) VALUES("MUNICIPAL");
INSERT INTO "tipo"(nome) VALUES("PRIVADA");
INSERT INTO "tipo"(nome) VALUES("FEDERAL");

Using this syntax, you assign only one or more specific fields, omitting those that do not need to be assigned.

Note: If this is a script to populate the database, it may be the case that you do not run it more than once. Otherwise, each time you do this a new line will be created for each of them, again and again ...

    
23.03.2014 / 21:39