How do you insert into an sqlite table with foreign key [closed] - sqlite

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hello I need help with an sqlite/Qt problem. The schema for the table is
CREATE TABLE PROJECT (
"pro_id" INTEGER NOT NULL DEFAULT 1,
"app_id" INTEGER NOT NULL DEFAULT 3,
"Start" TEXT NOT NULL,
"End" TEXT NOT NULL,
"Output" INTEGER NOT NULL,
"Log Type" INTEGER NOT NULL,
"Errors" INTEGER NOT NULL,
"Success" INTEGER NOT NULL,
"NOTES" VARCHAR NOT NULL DEFAULT '',
"Duration" TEXT NOT NULL DEFAULT '00.00.00.000',
PRIMARY KEY("app_ID", "pro_ID", "Start"),
FOREIGN KEY ("Output") REFERENCES "OUTPUTS" ("ID") ,
FOREIGN KEY ("app_ID", "pro_ID") REFERENCES "PROJECTS"("A_ID", "P_ID"),
FOREIGN KEY ("Log Type") REFERENCES "TYPE" ("ID"));
The INSERT query (inside Qt) is
temp = "INSERT INTO PROJECT (pro_id, app_ID,
Start, End, Output, Log Type, Errors, Success, NOTES, Duration)
VALUES(1,1, Date & time in quotes, Date & time in double quotes, '1, 1, 0, 0, '0.0.0.0.000')";
The error I get is:
QSqlError("1", "Unable to execute", "near \"Type \": syntax error")
I feel like the problem is on the sqlite side and I'm not familiar with it at all. I started it only this year. I feel like the foreign key is where the issue is

You should probably fire up sqlite and try some things out. Your error is just saying that it doesn't know what "Type" is most likely because of the space. If you wrap "Log Type" in quotes, you'll get something else.
But you have other errors in that statement from what I can tell. But I can't tell if you are abbreviating on purpose like "Date & time in quotes" or not. There's also a single quote missing potentially, etc.
So assuming the values are 100% correct, inserting into a table with a foreign key is just a matter of knowing for sure that the key exists in the foreign key table.

Related

nested insert function not working sqlite3 node js [duplicate]

I'm trying out statements for creating a database, and after 10 entities without any issues I ran into this error
Error: Near line 83: near "Transaction": syntax error
The first line is line 83 with it's context of creating a table
CREATE TABLE Transaction (
TransactionID INTEGER,
AccountID INTEGER REFERENCES User (AccountID),
ItemID INTEGER REFERENCES Item (ItemID),
Method STRING,
Price INTEGER,
TransactionDate DATE,
PRIMARY KEY (TransactionID)
);
Now I can't seem to find the issue, and suggestion's of something with ASCII using the wrong space couldn't be solved by writing the same thing again manually.
I haven't even gotten around to checking the integrity of my foreign keys, and it's not working. Hopefully somebody could provide some insight on what I'm missing.
Transaction is one of the reserved names in SQLite. For a full list see here.
Ways to solve this issue are:
Change the Table name to a word that isn't reserved.
or
Quote the reserved name by using one of these 4 listed quote marks
'keyword'
"keyword"
[keyword]
`keyword`

Python with Sqlite3 Syntqx error doing a select query with where condition where parameter is a variable of type integer [duplicate]

I'm trying out statements for creating a database, and after 10 entities without any issues I ran into this error
Error: Near line 83: near "Transaction": syntax error
The first line is line 83 with it's context of creating a table
CREATE TABLE Transaction (
TransactionID INTEGER,
AccountID INTEGER REFERENCES User (AccountID),
ItemID INTEGER REFERENCES Item (ItemID),
Method STRING,
Price INTEGER,
TransactionDate DATE,
PRIMARY KEY (TransactionID)
);
Now I can't seem to find the issue, and suggestion's of something with ASCII using the wrong space couldn't be solved by writing the same thing again manually.
I haven't even gotten around to checking the integrity of my foreign keys, and it's not working. Hopefully somebody could provide some insight on what I'm missing.
Transaction is one of the reserved names in SQLite. For a full list see here.
Ways to solve this issue are:
Change the Table name to a word that isn't reserved.
or
Quote the reserved name by using one of these 4 listed quote marks
'keyword'
"keyword"
[keyword]
`keyword`

Change a Column to Primary Key in sqlite [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table in my app
CREATE TABLE VisitDetails (VisitId INTEGER, UserId TEXT, VisitNumber
TEXT, JobId INTEGER, JobNumber TEXT, StatusId INTEGER, Status TEXT,
SignatureRequired BOOL, StatusDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
,StartDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, EndDate TIMESTAMP
DEFAULT CURRENT_TIMESTAMP, IsPPMJob BOOL)
and it was working fine but now I need to alter this table to make already existing column 'VisitId' to become the primary key. Can somebody help me please? It might work by adding Unique constraint to VisitId column of the table but I am trying to put some efficient solution !
You can't change SQLite tables field's primary key once the table is created,
Possible solution is,
Create new table with desired primary key
Copy all data
Drop old table
Here is the documentation link : https://www.sqlite.org/omitted.html

SQL Lite Issue - Check ('<=now')

Good Afternoon, I'm struggling with including an input mask into my SQL Lite code as per below -
PRAGMA foreign_keys=on;
CREATE TABLE User
(Customer_ID INTEGER PRIMARY KEY AUTOINCREMENT,
Cust_Name vchar(50) NOT NULL,
Cust_Add vchar(100) NOT NULL,
Cust_Town vchar(50) NOT NULL,
Cust_PC vchar(7) NOT NULL,
Cust_DOB DateTime DEFAULT CURRENT_DATE CHECK ('<=now') NOT NULL,
Cust_Tel vchar(13) NOT NULL,
Cust_eMail vchar(50));
I'm trying to get the SQL database to not allow entries of DOB's that are greater or equal to today, however when I try to import data, I get an error constraint. I can't see where I'm going wrong? The DOB's are most certainly pre 2017, 1985 to be exact. Any help or suggestions would be greatly appreciated before I rip my hair out.
The CHECKconstraint should be
CHECK(Cust_DOB <= date('now'))
See sqlite documentation:
Each time a new row is inserted into the table or an existing row is updated, the expression associated with each CHECK constraint is evaluated and cast to a NUMERIC value in the same way as a CAST expression. If the result is zero (integer value 0 or real value 0.0), then a constraint violation has occurred. If the CHECK expression evaluates to NULL, or any other non-zero value, it is not a constraint violation.
What happened in your statement is CAST('<= now' AS NUMERIC) evaluates to 0 (what you intended as expression is actually a string, that does not start with something that can be interpreted as a number).

View Creation in SQLite

Having viewed several matrials both here on Stack Overflow and other external sources, I am attempting to write a piece of code to generate a view that performs a calculation utilising data from other tables, The aim of the code is:
To Acquire username from the UserDetails table
Acquire weight in pounds from the UserDetails Table
Acquire distance_in_miles from the Cardiovascular Records table
Use the weight and distance in a calculation as shown below which is output as the caloriesBurned column.
My attempt can be seen below:
CREATE VIEW CardioCaloriesBurned (username, weight, distance, caloriesBurned) AS
SELECT UserDetails.username, UserDetails.weight_in_pounds,
CardiovascularRecords.distance_in_miles ,
((0.75 X weight_in_pounds) X distance_in_miles)
FROM UserDetails, CardiovascularRecords
If anyone could help in correcting this issue It would be greatly appreciated.
Edit: I am getting a syntax error in SQLite Manager relating to a "(" however Im not seeing any issues myself.
Edit: Code for the Cardiovascular Table and UserDetails table below:
CREATE TABLE "CardiovascularRecords" ("record_ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE CHECK (record_ID>0) , "distance_in_miles" REAL NOT NULL CHECK (distance_in_miles>0) , "username" TEXT NOT NULL , "date" DATETIME DEFAULT CURRENT_DATE, "notes" TEXT(50), "start_time" TEXT, "end_time" TEXT, FOREIGN KEY(distance_in_miles) REFERENCES DistanceinMiles(distance_in_miles) ON DELETE CASCADE ON UPDATE CASCADE,FOREIGN KEY(username) REFERENCES UserDetails(username) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE "UserDetails" ("username" TEXT PRIMARY KEY NOT NULL UNIQUE CHECK (length(username)>0), "password" TEXT NOT NULL CHECK (length(password)>3), "email_address" TEXT NOT NULL UNIQUE CHECK (length(email_address)>3) , "weight_in_pounds" REAL NOT NULL CHECK(weight_in_pounds>0) CHECK (length(weight_in_pounds)>0), "height_in_inches" REAL NOT NULL CHECK(height_in_inches>0) CHECK (length(height_in_inches)>0), "age" INTEGER CHECK(age>0), WITHOUT ROWID)
Thanks
JHB92
Your immediate error message is probably caused by the fact that you're using the letter X to indicate multiplication. It should be * (the capital 8 character on US keyboards).
After you've solved that problem, you may find that you have many, many more results in the output than you expect. That's because you're not telling the database which record in CardiovascularRecords to "hook up" for the purpose of doing the calculation. That may or may not (probably not) produce the results you want.
If you're not getting what you want, you must tell us which column or columns in UserDetails "point to" the record in CardiovascularRecords that applies to any given calculation.
CREATE VIEW CardioCaloriesBurned AS
SELECT UserDetails.username AS username,
UserDetails.weight_in_pounds AS weight,
CardiovascularRecords.distance_in_miles AS distance,
((0.75 * weight_in_pounds) * distance_in_miles) AS caloriesBurned
FROM UserDetails INNER JOIN CardiovascularRecords
ON UserDetails.username = CardiovascularRecords.username

Resources