Home > Enterprise >  My Flask app returns an unexpected syntax error
My Flask app returns an unexpected syntax error

Time:08-11

I'm basically building a secured online diary application with Flask. However my Python source code returns a syntax error when I try to test the app. I can't detect what's wrong with the syntax. Your help will be appreciated.

I'm attaching a screenshot of the error. And here's my SQL database's schema:

CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username TEXT NOT NULL,
hash TEXT NOT NULL
);

CREATE TABLE sqlite_sequence(name,seq);

CREATE UNIQUE INDEX username ON users (username);

CREATE TABLE diaries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
description TEXT NOT NULL,
img_url TEXT,
FOREIGN KEY(user_id) REFERENCES users(id)
);

screenshot

New error: unsupported value

screenshot2

CodePudding user response:

It is INSERT statement that causes error.

Well, not the insert itself but the way you're using it.

Values should be passed as a tuple (values between "(" and ")")

So, you need to update db.execute line with something like that

db.execute("insert into table_name(col1, col2) values(?, ?)", (col1_val, col2_val))

UPD. regarding the error on second screenshot.

db.execute("Select...) does not return a value but a set of values.

So, you might wanted to use fetchone() as in docs

res = cur.execute('SELECT count(rowid) FROM stocks') # gets you set records
print(res.fetchone()) # get first record

Anyway, check the docs I provided you link to with.

  • Related