Wednesday, April 9, 2014

Seeking advice. SQLite data storage error


This is what I realized SQLiteOpenHelper class, and perform only what onCreate method statement:
 sqLiteDatabase.execSQL ("CREATE TABLE user (id int primary key, name varchar (10))"); 



This is the statement I executed activity:
 MySqlite (SqliteActivity.this, "earlDatabase.db", null, 1); 
SQLiteDatabase write = mySqlite.getWritableDatabase ();
ContentValues ​​values ​​= new ContentValues ​​();
values.put ("id", 1);
values.put ("name", "earl");

write.insert ("user", null, values);
Toast.makeText (SqliteActivity.this, "insert the value of success ______" + values.get ("id"), 1200) show ();.

values.put ("id", 2);
values.put ("name", "abc");

write.insert ("user", null, values);
Toast.makeText (SqliteActivity.this, "insert the value of success ______" + values.get ("id"), 1200) show ();.


But the error. First run without error, for the first time after being given. Although the error, but the database has data inserted. Here is the error:




Seek expert advice, in the end what's wrong?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
ID This field what attributes? Is the primary key right? Should not duplicate values
Reply:
id int primary key, said id is unique primary key can not have duplicate values ​​are inserted, should start to detect the largest database ID, and then add 1, then insert.
Reply:
I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
Reply:
reference to the third floor wang584406938 reply:
I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default The current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error
Reply:
reference to the second floor tcmakebest reply:
id int primary key, said id is unique primary key can not have duplicate values ​​are inserted, you should start with the largest database found The ID, then add 1, and then insert.
I also tried to set id is not a primary key, but still error. Do not know why?
Reply:
references, 4th Floor jie873440996 reply:
Quote: references to the third floor wang584406938 reply:

I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error


I mean, you create a table with a primary key ID after, when data is added, it is not assigned to the id, sqlite will be automatically assigned to the primary key column, I remember this, you can try
Reply:
reference to the 6th floor wang584406938 reply:
Quote: references to the 4th floor jie873440996 reply:

Quote: references to the third floor wang584406938 reply:

I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error


I mean, you create a table with a primary key ID after, when data is added, it is not assigned to the id, sqlite will be automatically assigned to the primary key column, I remember this, you can try

Positive solution.
The second insert the same data, because id already exists, so an exception is thrown.
Reply:
reference to the 7th floor tantahe reply:
Quote: references to the 6th floor wang584406938 reply:

Quote: references to the 4th floor jie873440996 reply:

Quote: references to the third floor wang584406938 reply:

I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error


I mean, you create a table with a primary key ID after, when data is added, it is not assigned to the id, sqlite will be automatically assigned to the primary key column, I remember this, you can try

Positive solution.
The second insert the same data, because id already exists, so an exception is thrown.
but I changed the construction of the table, the primary key or error. So why would an error
Reply:
reference to the 7th floor tantahe reply:
Quote: references to the 6th floor wang584406938 reply:

Quote: references to the 4th floor jie873440996 reply:

Quote: references to the third floor wang584406938 reply:

I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error


I mean, you create a table with a primary key ID after, when data is added, it is not assigned to the id, sqlite will be automatically assigned to the primary key column, I remember this, you can try

But I changed the sentence to build the table, in addition to the primary key, or an error. And ContextValues ​​not write "cv.put (" id ", null);", can not be set to a null value.

Reply:
reference to the 9th floor jie873440996 reply:
Quote: references to the 7th floor tantahe reply:

Quote: references to the 6th floor wang584406938 reply:

Quote: references to the 4th floor jie873440996 reply:

Quote: references to the third floor wang584406938 reply:

I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error


I mean, you create a table with a primary key ID after, when data is added, it is not assigned to the id, sqlite will be automatically assigned to the primary key column, I remember this, you can try

But I changed the sentence to build the table, in addition to the primary key, or an error. And ContextValues ​​not write "cv.put (" id ", null);", can not be set to a null value.

primary key is not removed, you should cv.put ("id", ***); these two removed.

Reply:
reference to the 10th floor tantahe reply:
Quote: references to the 9th floor jie873440996 reply:

Quote: references to the 7th floor tantahe reply:

Quote: references to the 6th floor wang584406938 reply:

Quote: references to the 4th floor jie873440996 reply:

Quote: references to the third floor wang584406938 reply:

I remember that if you define a primary key, it does not give the assignment id assignment, sqlite will automatically add the value of the primary key column, the default current maximum plus 1
so? The first use SQLite. I also tried to set the id is not a primary key, but still error


I mean, you create a table with a primary key ID after, when data is added, it is not assigned to the id, sqlite will be automatically assigned to the primary key column, I remember this, you can try

But I changed the sentence to build the table, in addition to the primary key, or an error. And ContextValues ​​not write "cv.put (" id ", null);", can not be set to a null value.

primary key is not removed, you should cv.put ("id", ***); these two removed.

But I would like to insert a database value, remove the put method, how to insert data into the database?

Reply:
Primary key id set to automatically increase.
You want to insert the value is the name thing.
Each insert a name, id is automatically incremented by 1.

No comments:

Post a Comment