Problem with internal database

Asked

Viewed 159 times

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 mistake I’m having is this::

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 already tried some things: take the number I myself was entering (for being with an autoincrement), but did not give and continued the same mistake... Someone knows what’s wrong?

  • 2

    In INSERT you either put the list of fields (omitting the code field that will be generated automatically) or you have to enter the value of all fields (which you did not do in the first INSERT). Clean your table and redo the Inserts in a single way. To check what has been included run a SELECT * FROM "type" .

1 answer

1


Is your database empty? Your field código is auto-increment, so if you have a single record that is then the code 1 will already be "busy". The ideal 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 field, omitting those that need not be assigned.

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

  • Man, thanks a lot! I wasn’t noticing these silly mistakes I was making, I was thinking it could be SQL Server problem, I was freaking out... I tidied up and now it’s all going well, really worth!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.