OperationalError: near "(": syntax error" at SQLite3 - sqlite

After taking a crash course for SQLite3, I tried to make a db for my first project:
import sqlite3 as db
conn = db.connect('todo.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE todo(id serial primary key, title text, created
timestamp default now(), done boolean default 'f')")
cursor.execute("INSERT INTO todo (title) VALUES('Learn web.py')")
Unfortunately I receive this error:
OperationalError: near "(": syntax error" at SQLite3
I do not understand what's wrong with the code. Can anyone explain what I am doing wrong?

As shown in the documentation, if the default value is not a simple value, it must be enclosed in parentheses:
CREATE TABLE todo(
...,
created timestamp default (now()),
done boolean default 'f'
);
(And 'f' is not a valid value for a boolean. And now() is not an SQLite function.)

Related

NULL statement doesn't fill the empty spaces in Sqlite

I'm having trouble with NULL statement in SQLITE. I added NULL in cases there is no info to be filled, but once I run the code the IDE throws an error.
CREATE TABLE tenants (
Apartment_Number INT(4),
Family_Name VARCHAR(8) NULL,
Sur_Name VARCHAR(14) NULL,
Home_Number INT(4),
Mobile_Number int(10),
PRIMARY KEY (Apartment_Number )
);
INSERT INTO tenants
VALUES
(101,,,201,0544431263),
(102,,,202,0544431263),
(103,'Shklobin','marta',203,0544431263),
(104,'arman','charles',204,0544431263);
SELECT * FROM tenants;
The empty spaces are where I hope the IDE will fill with NULL values.
The error I receive:
Error: near line 12: near ",": syntax error.
If I remove the NULL statement, the IDE runs the code with no errors.
Official documentation indicates that
The default value of each column is NULL.
The default behavior is also to allow NULL in each column. The behavior only changes if NOT NULL and/or DEFAULT ... constraints are specified. You should get the same error whether or not you have the lone NULL keyword as shown in the question code. My testing shows that the following does not suppress the error as implied in the question--in other words, the following change results in the same error.
Family_Name VARCHAR(8),
Sur_Name VARCHAR(14),
The following alternative INSERT statements will work:
INSERT INTO tenants
(Apartment_Number, Home_Number, Mobile_Number)
VALUES
(101,201,0544431263),
(102,202,0544431263);
INSERT INTO tenants
VALUES
(103,'Shklobin','marta',203,0544431263),
(104,'arman','charles',204,0544431263);
or
INSERT INTO tenants
VALUES
(101, NULL, NULL,201,0544431263),
(102, NULL, NULL,202,0544431263),
(103,'Shklobin','marta',203,0544431263),
(104,'arman','charles',204,0544431263);

Error when inserting into Database in Python

What is wrong with the code to produce this error
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s)' at line 1
I am using XAMPP Localhost Database and mysql.connector Package in Python. My Code for Inserting:
cursor = db.cursor()
username = input()
sql = "INSERT INTO `employee` (`id`, `username`) VALUES (NULL, %s)"
cursor.execute(sql, username)
db.commit()
print("success")
Instead of this line
cursor.execute(sql, username)
try this
cursor.execute(sql, [username])
execute expects a list/(list of tuples) as the second parameter. You can have a look at the documentation if you need more details.

SQLite delete from table where condition is of boolean type

I am trying to run this query without success :
DataModule1.UniQuery5.Close;
DataModule1.UniQuery5.SQL.Clear;
DataModule1.UniQuery5.SQL.Text:='delete from mytable where job_done = "1"';
DataModule1.UniQuery5.ExecSQL;
Job_done is a boolean field.
Since SQLite has only 2 conditions for true/false (0 or 1), I am failing to understand why nothing gets deleted. Can you help me ?
try this command
delete from mytable where job_done = 1;
without quotes
It seems that sqlite does not have boolean type fields. So I changed the type to wideinteger.Now unchecked marks to '0' and checked to '-1'. Now,this way, everything works.

How to set the ABORT option in SQLite from Tcl when violating NOT NULL constraint

I have a TCL script where i generate a SQLite table:
DB eval {CREATE TABLE StressDat2( LC int NOT NULL, EID int NOT NULL, Xtens float, Ytens float ) }
When I try to write NULL values they get accepted anyhow. How can I from Tcl, when generating my table, set the ABORT option which shall handle writing attempts of NULL values?
The Xtens and Ytens columns do not have a NOT NULL constraint.
(The default conflict resolution algorithm is ABORT; you don't need to set it.)
It depends on how do you insert data into the table. If you also do it with Tcl, you need to be aware, that Tcl doesn't understand the idea of NULL value.
Therefore, this is WRONG:
set lc ""
set eid ""
set xtens ""
set utens ""
DB eval {INSERT INTO StressDat2 (LC, EID, Xtens, Ytens) VALUES ($lc, $eid, $xtens, $ytens);
You're obviously inserting empty strings, not null values. To insert null, use null keyword in SQL statement.
This is CORRECT:
DB eval {INSERT INTO StressDat2 (LC, EID, Xtens, Ytens) VALUES (NULL, NULL, NULL, NULL);
Finally, a word about "ABORT" conflict resolution in NOT NULL constraint, that you mentioned. You said this is blank in SQLiteStudio and not "ABORT", as you would like to. Well, the "ABORT" algorithm is a default algorithm used by sqlite when no algorithm was defined, so even you have it blank (default), it means it's "ABORT". Read this for more details: http://sqlite.org/lang_createtable.html

How do I use a boolean field in a where clause in SQLite?

It seems like a dumb question, and yet. It could be my IDE that's goofing me up. Here's the code (this is generated from DbLinq):
SELECT pics$.Caption, pics$.Id, pics$.Path, pics$.Public, pics$.Active, portpics$.PortfolioID
FROM main.Pictures pics$
inner join main.PortfolioPictures portpics$ on pics$.Id = portpics$.PictureId
WHERE portpics$.PortfolioId = 1 AND pics$.Id > 0
--AND pics$.Active = 1 AND pics$.Public = 1
ORDER BY pics$.Id
If I run this query I get three rows back, with two boolean fields called Active and Public. Adding in the commented out line returns no rows. Changing the line to any of the following:
pics$.Active = 'TRUE'
pics$.Active = 't'
pics$.Active = boolean(1)
It doesn't work. Either errors or no results. I've googled for this and found a dearth of actual SQL queries out there. And here we are.
So: how do I use a boolean field in a where clause in SQLite?
IDE is SQLite Administrator.
Update: Well, I found the answer. SQLite Administrator will let you make up your own types apparently; the create SQL that gets generated looks like this:
CREATE TABLE [Pictures] ([Id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[Path] VARCHAR(50) UNIQUE NOT NULL,[Caption] varchAR(50) NULL,
[Public] BOOLEAN DEFAULT '0' NOT NULL,[Active] BOOLEAN DEFAULT '1' NOT NULL)
The fix for the query is
AND pics$.Active = 'Y' AND pics$.Public = 'Y'
The real issue here is, as the first answerer pointed out, there is no boolean type in SQLite. Not an issue, but something to be aware of. I'm using DbLinq to generate my data layer; maybe it shouldn't allow mapping of types that SQLite doesn't support. Or it should map all types that aren't native to SQLite to a string type.
You don't need to use any comparison operator in order to compare a boolean value in your where clause.
If your 'boolean' column is named is_selectable, your where clause would simply be:
WHERE is_selectable
SQLite does not have the boolean type: What datatypes does SQLite support?
The commented-out line as it is should work, just use integer values of 1 and 0 in your data to represent a boolean.
SQLite has no built-in boolean type - you have to use an integer instead. Also, when you're comparing the value to 'TRUE' and 't', you're comparing it to those values as strings, not as booleans or integers, and therefore the comparison will always fail.
Source: http://www.sqlite.org/datatype3.html
--> This Will Give You Result having False Value of is_online field
select * from device_master where is_online!=1
--> This Will Give You Result having True Value of is_online field
select * from device_master where is_online=1

Resources