Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for How to stop my primary key ID from incrementing?

$
0
0

How can I use the primary key multiple times?

You can't. A primary key must be unique.

How do I get my ID to stay as 62381 for example within userInput.primarykey and not increment?

Don't. Leave the ID as is and do not insert your own. integer primary key is special in SQLite and will provide a good unique primary key by default. Other databases would require you to declare it auto-increment or identity.

Instead, add another column to store userLogin.primarykey.

I can only guess that userLogin.primarykey is the ID of the user associated with that Food Tracker row. It should be "User ID" and declared as a foreign key referencing your Users table.

create table "Food Tracker" (  id integer primary key,"User ID" integer not null references Users(id),  -- I notice you're not using this in your insert.  -- What is it?"Entry Number" integer,"Calories" integer,"Food Item" text not null,"Date" text,"Time" text)

This assumes you have a Users table to refer to and that each userLogin.primarykey has an entry. You may need to insert the users before you insert their food tracker info.


Note1: your insert has 5 values, but you're passing in 6.

Note2: Be careful with CREATE TABLE IF NOT EXISTS. If you change your table definition it will do nothing; you will be using the old definition. Instead, for a little test database like this, drop the table and recreate it.

Note3: Column names with spaces in them is asking for trouble, spaces are used to separate words. The result is you have to quote everything, which has its own problems, for example your insert forgets the quotes. In addition, most databases ignore case so thing and Thing refer to the same thing, but "Thing" might only refer to Thing and not thing. Consider using thisStyle or this_style.

Note4: Store date and time in a single column, "Tracked At". This takes less storage and makes working with the data much, much, much easier. Importing data is when you should fix these issues, combine the date & time before inserting. Also get used to using the appropriate type, datetime or timestamp (SQLite's loose handling of types is unusual).


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>