INSERT INTO wagers (id, creator_addr, title, platform,
region, match, wager_amount, currency, status, isDeleted)
VALUES ("a033bbae-e472-4f86-a463-844358511aa4", "addr", "title", "platform",
"region", "match", 100, "USD", 1, 0)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match, wager_amount, currency, status, isDeleted) VALUES ("a033bbae-e472-4f86-a4' at line 1
DB Table: https://i.gyazo.com/947505729979cd9c756aaf0e5ffc9b43.png
I think this is because "match" is an SQL statement (like "WHERE" and "FROM").
Try it with the column name between backticks:
INSERT INTO wagers (id, creator_addr, title, platform, region, `match`, wager_amount, currency, status, isDeleted) VALUES ("a033bbae-e472-4f86-a463-844358511aa4", "addr", "title", "platform", "region", "match", 100, "USD", 1, 0);
mazch is ine of the reserved words
You can use backticks for column names, so tat it will not cause a problem
INSERT INTO wagers (id, creator_addr, title, platform, region, `match`, wager_amount, currency, status, isDeleted)
VALUES ("a033bbae-e472-4f86-a463-844358511aa4", "addr", "title", "platform", "region", "match", 100, "USD", 1, 0)
Related
I am wondering why in the following example my Sqllite3 in Python command does not work:
I am creating the following table
query = """
CREATE TABLE customers2 (
"customer_id" serial PRIMARY KEY,
"name" VARCHAR UNIQUE,
"name2" VARCHAR UNIQUE,
"email" VARCHAR NOT NULL,
"active" bool NOT NULL DEFAULT TRUE,
constraint test unique(name, name2)
)
"""
cur.execute(query)
Then I am inserting some values
query = """
INSERT INTO customers2(name, name2, email)
VALUES
('IBM', 'a','contact#ibm.com'),
('Microsoft','b', 'contact#microsoft.com'),
('Intel', 'c','contact#intel.com');
"""
cur.execute(query)
Then I would like to insert a combination of name = "Microsoft" and name2 = "a" which values already exist but not in combination. The following command gives an error
query = """
INSERT INTO customers2 (name, name2, email)
VALUES('Microsoft','a','hotline#microsoft.com')
ON CONFLICT (name, name2)
DO UPDATE SET name2 = name2+"b";
"""
cur.execute(query)
IntegrityError: UNIQUE constraint failed: customers2.name2
What is the reason for this?
Printing out the data gives me:
\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00\x84\x00\t\x06\x07\x13\x13\x12\x15\x13\x13\x13\x16\x16\x15\x17\x19\x1a\x1b\x17\x16\x18\x18
So the binary data is present. However, when I try inserting the value with this code:
for i in check_row:
if i[0] == 0:
db.execute("INSERT INTO profiles (profile_id, city, country, interest, bio, picture) VALUES (:user, :city, :country, :interest, :bio, :picture)",
[user, city, country, interest, bio, empPicture])
conn.commit()
flash("profile completed")
return redirect("/")
else:
db.execute("UPDATE profiles SET city = :city, country = :country, interest = :interest, bio = :bio, picture = :picture WHERE profile_id = :profile_id",
[city, country, interest, bio, user, empPicture])
conn.commit()
flash("profile updated")
return redirect("/profile")
empPicture is the variable holding the binary data. This returns no error however, data never gets inputted into the picture BLOB column, it just shows NULL and prints out None when ran.
This is my table:
CREATE TABLE "profiles" ( "profile_id" INTEGER, "city" TEXT VARCHAR(255), "country" TEXT VARCHAR(255), "interest" TEXT VARCHAR(255), "bio" TEXT VARCHAR(255), "picture" BLOB )
I believe you have to wrap your insert into sqlite3.Binary as follows:
db.execute("INSERT INTO profiles (profile_id, city, country, interest, bio, picture) VALUES (:user, :city, :country, :interest, :bio, :picture)",
[user, city, country, interest, bio, sqlite3.Binary(empPicture)])
conn.commit()
More details can be found here.
I have a line of code something like this:
query <- "INSERT INTO my_books (title, subtitle, content) VALUES ($1,$2,$3)"
data_to_be_inserted <- c("hello", NA, "world")
dbExecute(db_connection_to_postgresql, query, data_to_be_inserted)
When doing this, the values in the db are:
SELECT * FROM my_books;
id title subtitle content
1 "hello" "NA" "world"
instead of:
SELECT * FROM my_books;
id title subtitle content
1 "hello" NULL "world"
Why is this?
That is because you didn't insert NULL, but NA.
You could use
query <- "INSERT INTO my_books (title, subtitle, content) VALUES ($1, nullif($2, 'NA'), $3)"
I have information in an SQLite database. The database structure can not be changed.
I am trying to construct a query that will give me a result in which the TypeOfInformation entries are field names:
My first try was to work with subqueries:
SELECT (SELECT Value FROM FinData WHERE Type = 'Price') AS Price,
(SELECT Value FROM FinData WHERE Type = 'Volume') AS Volume
FROM FinData")
Seemed perfect, however, the result was a resultset in which EVERY entry in the columns Price and Volume are equal to the FIRST respective entry of Price and Volume in the original database:
I tried to get around this and to include the other Price and Volume information - but I failed. (Which is a pity, because the syntax seemed somehow easy to grasp.)
Next try was the following:
Select Date, Value AS Volume From FinData WHERE Volume IN
(SELECT Value FROM FinData WHERE (Type = 'Volume'))
This gives me a resultset with a Volume column and all volume information. Okay, so far. However, when I want to complement this resultset which a Price column via
Select Date, Value AS Volume From FinData WHERE Volume IN
(SELECT Value FROM FinData WHERE (Type = 'Volume'))
union
Select Date, Value AS Close From FinData WHERE Price IN
(SELECT Value FROM FinData WHERE (Type = 'Price'))
I get a resultset that shows Price and Volume information in only ONE column ("Volume"), which therefore is also useless.
To look up a value corresponding to a row in the outer query, you have to use a correlated subquery, which explicitly makes a connection between both:
SELECT Date,
(SELECT Value
FROM FinData
WHERE Date = Dates.Date
AND TypeOfInformation = 'Price'
) AS Price,
(SELECT Value
FROM FinData
WHERE Date = Dates.Date
AND TypeOfInformation = 'Volume'
) AS Volume
FROM (SELECT DISTINCT Date
FROM FinData) AS Dates;
(The DISTINCT subquery is used to prevent multiple rows for each date.)
Alternatively, group all rows for a date, and use aggregation functions and CASE expressions to extract the values from the proper rows:
SELECT Date,
MAX(CASE WHEN TypeOfInformation = 'Price' THEN Value END) AS Price,
MAX(CASE WHEN TypeOfInformation = 'Volume' THEN Value END) AS Volume
FROM FinData
GROUP BY Date;
Assuming dates are unique per price volume pair, you can do this:
with xxx(date,price,volume) as
(
select date,value,0 from findata where typeofinformation = 'Price'
union
select date,0,value from findata where typeofinformation = 'Volume'
)
select date,sum(price) price,sum(volume) volume from xxx group by date;
I've got the below sqlite database:
CREATE TABLE `test` (
`idx` INTEGER PRIMARY KEY AUTOINCREMENT,
`qty` INTEGER,
`brand` TEXT,
`type` TEXT
);
insert into test (qty, brand, type) values (1, "Merc", "petrol");
insert into test (qty, brand, type) values (1, "Toyota", "diesel");
I want to insert another record but if it exists then I want to increase the qty field, so:
insert into test (qty, brand, type) values (1, "Merc", "diesel");
so would just increase the qty field by one, and
insert into test (qty, brand, type) values (1, "Merc", "petrol");
would insert a new record.
SQLite is an embedded database, i.e., it is designed to be used together with an application.
The easiest way is to put the logic into the application:
db.execute("UPDATE test SET qty = qty + 1 WHERE brand = ? AND type = ?",
["Merc", "diesel"])
if db.rows_affected == 0:
db.execute("INSERT ...")
As of version 3.24.0, SQLite supports UPSERT.
CREATE TABLE `test` (
`idx` INTEGER PRIMARY KEY AUTOINCREMENT,
`qty` INTEGER DEFAULT 1,
`brand` TEXT,
`type` TEXT
)
UNIQUE (brand, type)
;
INSERT INTO test (qty, brand, type) VALUES ("Merc", "diesel")
ON CONFLICT(brand, type) DO UPDATE SET qty = qty + 1;
INSERT INTO test (qty, brand, type) VALUES ("Merc", "diesel")
ON CONFLICT(brand, type) DO UPDATE SET qty = qty + 1;
SELECT qty, brand, type FROM test;
Output:
2 Merc diesel