0

Can anyone help me in Sqlite query

I created column 'ID' and gave constraint as 'Primary key', While inserting the values for other columns it is throwing an error to pass the value for 'ID'.

21st Sep 2022, 7:10 AM
Sultan Shaikh
Sultan Shaikh - avatar
12 Answers
+ 3
Tibor Santa and [user id="4354920"]CalviÕ²[/user] Thanks for your help
22nd Sep 2022, 1:02 PM
Sultan Shaikh
Sultan Shaikh - avatar
+ 2
Sultan Shaikh you can also manually specify the value of the ID if you want, when you do the insert. By using AUTOINCREMENT, the database will assign a number to each new row, based on an algorithm. Now that Calvin has already told you what is the problem, you are only a Google search away from finding out more: https://www.sqlite.org/autoinc.html
21st Sep 2022, 11:33 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Set AUTOINCREMENT to the ID
21st Sep 2022, 7:21 AM
CalviÕ²
CalviÕ² - avatar
+ 1
@Calvin, Data type is int then why should we add autoincrement?
21st Sep 2022, 8:39 AM
Sultan Shaikh
Sultan Shaikh - avatar
+ 1
Tibor Santa I tried with manual entry, but it is allowing me for duplicate ID. I am not getting any error while entering the same ID which I already have.
21st Sep 2022, 11:47 AM
Sultan Shaikh
Sultan Shaikh - avatar
+ 1
Sultan Shaikh The primary key does enforce uniqueness. See sample code here, it's not possible to insert the same ID twice. https://code.sololearn.com/cZUM385ZGOa3/?ref=app
21st Sep 2022, 7:05 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa Why did you use executemany instead of using execute for INSERT query?
22nd Sep 2022, 5:22 AM
Sultan Shaikh
Sultan Shaikh - avatar
+ 1
It should not make any difference. Executemany has nice syntax to work on a data structure that contains all the relevant fields, and no need to write a loop. I copied and adjusted the example from python standard documentation. Maybe you have a problem with commit missing? You did not link your own code, so I can only guess. Maybe I shouldn't. :)
22nd Sep 2022, 12:02 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Sultan Shaikh Check out here how does the Animal table create data with ID auto increment set in Sqlite https://code.sololearn.com/wT34BpTg8sgH/?ref=app
22nd Sep 2022, 2:38 PM
CalviÕ²
CalviÕ² - avatar
0
Int data type is not auto increment by default
21st Sep 2022, 8:47 AM
CalviÕ²
CalviÕ² - avatar
0
Yes
24th Sep 2022, 11:58 AM
Mia
Mia - avatar
0
Mia I got the solution
24th Sep 2022, 1:44 PM
Sultan Shaikh
Sultan Shaikh - avatar