How to UPDATE table data = ? WHERE id = ? in SQLITE [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 5 days ago.
Improve this question
This is the error i'm getting:
RuntimeError: fewer placeholders (?) than values ('Joe', '7', '21', '6')
It is confirmed that the values Joe, 7, 21, 6 are passed through from my html form to python so it is some syntax error i believe.
I am trying to update a birthday entry in a database using the birthday id. I am able to run this query in SQLITE where I just type in the actual values instead of the qmark placeholders, so I am assuming its a syntax error in python. I've read the sqlite documentation and can't find any examples talking about UPDATE table data = ? WHERE id = ?
I can run other db query that are close to this in python without an error
This is what I would expect to work in python but it isnt..
I've tried every other syntactical variation of this I can think of as well
db.execute("UPDATE birthdays name = '?', month = '?', day = '?' WHERE id = '?'", name, month, day, id)
Because when I run the SQLITE query like this it works..
UPDATE birthdays name = 'Joe', month = '7', day = '21' WHERE id = 6;

Try without the ' around the placeholder. Placeholders on https://docs.python.org/3/library/sqlite3.html don't have any quotes around them even for string values.
db.execute("UPDATE birthdays SET name = ?, month = ?, day = ? WHERE id = ?", name, month, day, id)

Related

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

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.

Deleting rows past a certain row index [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have new data coming into the app all the time, so I want to limit the number of rows in a table to, say, 100 records. I would describe it as a FIFO queue. Whenever there new data (just a few rows a time) coming in, old data at the 'bottom' of the table are flushed out and deleted. Since it's FIFO, I don't want to manually perform a sort, then delete, then insert back in. I guess there must be cheap way to do this, right?
Thanks
A query like this will show all recors, newest first:
SELECT *
FROM MyTable
ORDER BY Date DESC -- or some autoincrementing ID column
With an OFFSET clause, you can skip the first records.
This means that you get all records except the first 100 ones, i.e., you get those records that should be deleted:
SELECT *
FROM MyTable
ORDER BY Date DESC
LIMIT -1 OFFSET 100
You can then use this in a subquery to actually delete the records:
DELETE FROM MyTable
WHERE ID IN (SELECT ID
FROM MyTable
ORDER BY Date DESC
LIMIT -1 OFFSET 100)

Update table using join and conditions(where) from another table [duplicate]

This question already has answers here:
How do I make an UPDATE while joining tables on SQLite?
(2 answers)
Closed 5 years ago.
I have two tables: Teams and Players.
Teams: TeamID, NumberOfPlayers, ..
Players: PlayerID, Name, Surname, TeamID, ..
I want to update NumberOfPlayers according to a player's Name, Surname and TeamID.
This is what I tried:
UPDATE Teams
SET NumberOfPlayers = 28
FROM Teams
JOIN Players ON Teams.TeamID = Players.TeamID
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
But I get an error:
near "FROM": syntax error: UPDATE Teams
SET NumberOfPlayers = 28
FROM
I also tried:
UPDATE Teams
JOIN Players ON Teams.TeamID = Players.TeamID
SET NumberOfPlayers = 28
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
And the error I get is:
near "JOIN": syntax error: UPDATE Teams
JOIN
Another shot was this:
UPDATE Teams AS T
JOIN Players P ON T.TeamID = P.TeamID
SET T.NumberOfPlayers = 28
WHERE P.Name LIKE 'Amata' AND P.Surname LIKE 'Walton' AND P.TeamID = 3
Which led me to this error:
near "AS": syntax error: UPDATE Teams AS
It seems like the problem is after UPDATE Teams, but that's a valid table.
Is this possible? What am I missing?
Your code is a bit odd, because you already know the count and the team ID you want to update, so I'm not sure why the player matters.
Further, I'd be concerned that it sorta looks like you might be using a firstname/lastname as a key, and I'd strongly not recommend that. Also, if you want an exact match on a string, = is referenced. See details on that here: Equals(=) vs. LIKE
But I believe this is the query you are technically going for.
Some relational databases support a FROM/JOIN on a update (like SQL Server), so don't (Like Oracle). SQLite doesn't support that syntax.
UPDATE Teams
SET NumberOfPlayers = 28
Where TeamId In
(SELECT TeamId From Players
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
)

Update in multiview using Linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to update details using linq to entities. But instead of takin a new aspx page i want to update details in another view. will it work.? and please give the linq to entity update query
Let us assume:
db is the context of your Database Entity.
Table_name is the name of Table you need to update.
row_id is the value you are using to search for the data in the Table.
To update using linq you need to fetch the record first using the below query:
var data = (from r in db.Table_name
where r.id == row_id
select r).FirstOrDefault();
Now to update the values just update them. For example:
data.Name = "Firstname lastname"
data.IsActive = true;
.
.
and so on
After you have updated the values in data you need to Save the changes made by you by this command:
db.SaveChanges();
That's it.

last record from database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Get the last record from a Microsoft SQL database table using ASP.net (VB) onto a web form.
I'm assuming he's trying to retrieve the last inserted record. As Ariel pointed out, the question is rather ambiguous.
SELECT TOP 1 * FROM Table ORDER BY ID DESC
If you have an identity column called ID, this is easiest. If you don't have an identity PK column for example a GUID you wont be able to do this.
Here is a basic solution:
var order = (from i in db.orders
where i.costumer_id.ToString() == Session["costumer_id"]
orderby i.order_id descending
select i).Take(1).SingleOrDefault();
You need to be more specific with actually putting it onto a web form, but the SQL to get the last record is:
SELECT *
FROM TABLE_NAME
WHERE ID = (SELECT MAX(ID) FROM TABLE_NAME)
Where ID is your ID and TABLE_NAME is your table name.

Resources