This is just a small bite of my code but I hope it will be enough so solve my problem. Allow me to explain first.
So I am attempting to store information in a database using python and sqlite3. I can store stuff in the database but the file size of the database never increases and after a restart the database is cleared. I know that .commit works because I have used it in the past but it is not working now. My assumption is that I am out of scope of the database and I don't have the ability to write. Once again, my code runs and provides no errors, but it will not actually write to the database.
My Connection and Init Code:
def connect_db():
conn = sqlite3.connect(app.config['DATABASE'])
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = connect_db()
cursor = conn.cursor()
sql = 'create table if not exists users (id integer primary key autoincrement, username text not null, password text not null, admin boolean not null)'
cursor.execute(sql)
conn.commit()
So this code connects to the database and gets everything setup.
Here is a small portion of my code to add an item to the database. It works and it will add items to the "database" but the file size of the database does not increase and if I restart it wont see the new items. But as long as the application is open I have access to the items.
#app.route('/user/', methods=['GET', 'POST'])
def users():
conn = connect_db()
cursor = conn.cursor()
isAdmin = False
if (request.form.get('adminuser') != None):
isAdmin = True
cursor.execute('insert into users (username, password, admin) values (?, ?, ?)',
[request.form['username'], request.form['password'], isAdmin])
conn.commit()
return redirect(url_for('index'))
Edit I left This Out
DATABASE = '/tmp/database.db'
Edit A Crazy Simple Mistake.
Change
DATABASE = '/tmp/database.db'
To
DATABASE = './tmp/database.db'
There is an error in this line:
DATABASE = '/tmp/database.db'
Use this instead:
DATABASE = './tmp/database.db'
Related
I try to update my sqlite database using flask webhook.
It seems commands line work fine if I type manually in the python console but my flask webhook didn't update my SQLite database. It seems the apps fail at the "cursor.execute()" line.
here is my webhook code:
#app.route('/trendanalyser', methods=['POST'])
def trendanalyser():
data = json.loads(request.data)
if data['passphrase'] == config.WEBHOOK_PASSPHRASE:
#Init update variables
tastate = data['TrendAnalyser']
date_format = datetime.today()
date_update = date_format.strftime("%d/%m/%Y %H:%M:%S")
update_data = ((tastate), (date_update))
#Database connection
connection = sqlite3.connect('TAState15min.db')
cursor = connection.cursor()
#Database Update
update_query = """Update TrendAnalyser set state = ?, date = ? where id = 1"""
cursor.execute(update_query, update_data)
connection.commit()
return("Record Updated successfully")
cursor.close()
else:
return {"invalide passphrase"}
Can you please tell me what's wrong with my code ?
if it's can help, here is my database structure (my db creation):
#Database connection
conn = sqlite3.connect("TAState15min.db")
cursor = conn.cursor()
#Create table
sql_query = """ CREATE TABLE TrendAnalyser (
id integer PRIMARY KEY,
state text,
date text
)"""
cursor.execute(sql_query)
#Create empty row with ID at 1
insert_query = """INSERT INTO TrendAnalyser
(id, state, date)
VALUES (1, 'Null', 'Null');"""
cursor.execute(insert_query)
conn.commit()
#Close database connexion
cursor.close()
**I finally found the issue, webhooks need the full path to the SQLite database to work fine. I just start to code in python, it was a noob issue... **
I finally found the issue, webhooks need the full path to the SQLite database to work fine. I just start to code in python, it was a noob issue...
I can't seem to figure out why my code won't create a MySQL table.
Everything seems ok to me, although my knowledge is limited.
import mysql.connector
import bot_keys as bk
conn = mysql.connector.connect(
host = bk.dbHost,
user = bk.dbUser,
password = bk.dbPass,
database = bk.dbName
)
mycursor = conn.cursor()
def create_table():
sql_fs1 = ("SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO';"
"SET AUTOCOMMIT = 0;START TRANSACTION;"
"SET time_zone = '+00:00';"
"CREATE TABLE `test_table`(`id` int(10) NOT NULL PRIMARY KEY "
"AUTO_INCREMENT,`date_added` timestamp NOT NULL DEFAULT "
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,`test_info` "
"varchar(10) NOT NULL,`xxx` varchar(255) NOT NULL);")
mycursor.execute(sql_fs1, multi=True)
conn.commit()
create_table()
Can anyone point out where I am failing? Thanks in advance.
I would first check to see if your connection is actually working...
if conn.is_connected():
db_Info = conn.get_server_info()
print("Connected to MySQL Server version ", db_Info)
This assumes your bot keys have been properly parameter-ized, if i were you I would attempt to initially run w hard coded values, then parameter-ize once youve successfully run things....
The error will go away if you delete the line
conn.commit
The create table command causes an implicit commit,
https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
I have an insert statement.
conn = sqlite3.connect('WO1.db')
with conn:
cur1 = conn.cursor()
cur1.execute("insert into workorder (Title, Link, Status)
values (?,?,?)", ('iijiji', 'ijjijijj', '22jhhuhij'))
if conn:
conn.close()
The title and link columns had UNIQUE constraints on them and I was getting the following error and my program terminated.
sqlite3.IntegrityError: UNIQUE constraint failed:
But 1 new record inserted into the database which is what I wanted.
I then created a new table where the Title and Link columns didn't have a UNIQUE constraint.
I ran the program again and this time received no error however, the record was inserted into the table twice which explains the error when there was UNIQUE constraints on the Link and Title.
Is there any logical explanation as to why this insert statement is executing twice?
Note This is only one place in the program where a connection is established, a query is executed and then the connection is closed. There is no other interaction with this database in the program other than the normal configuration.
I haven't had any other sessions open with this database either other than within this application.
I'm running this query in the python file where the program is run from.
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
conn = sqlite3.connect('WO1.db')
with conn:
cur1 = conn.cursor()
cur1.execute("insert into workorder (Title, Link, Status) values
(?,?,?)", ('en24433', 'www.reddit.com', 'Not Completed'))
if conn:
conn.close()
migrate = Migrate(app, db)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='localhost', port=8080, debug= True)
Your need to remake your database access code.
Firstly you connect twice at the same db by using the with statement after conn.connect().
When a database is accessed by multiple connections, and one of the processes modifies the database, the SQLite database is locked until that transaction is committed.
I think that this is the reason for your error.
After you make the insert in database you need to commit the changes.
This method commits the current transaction. If you don’t call this method, anything you did since the last call to commit() is not visible from other database connections. If you wonder why you don’t see the data you’ve written to the database, please check you didn’t forget to call this method.
Be aware that close() does not automatically commit:
This closes the database connection. Note that this does not automatically call commit(). If you just close your database connection without calling commit()first, your changes will be lost!
Take a look at sqlite3 API docs
It worked when I put the database connection and insert statements into my index route rather than above the routes.
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
#app.route('/')
def index():
conn = sqlite3.connect('WO1.db')
with conn:
cur1 = conn.cursor()
cur1.execute("insert into work_order (Title, Link, Status) values (?,?,?)",
('iikii', 'ijkoijj', '66hhuhij'))
conn.close()
return render_template('index.html')
I am trying to connect to SQLITE database and it would seem that the code does create the database, but however it does not create the tables nor does it insert anything in them (most probably because they were never created)
I have SQLite DB Browser and in it I open the database but lack any tables or anything at all. Also I see my database in the folder next to my project.
I have the following code:
import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()
def create_tabe():
c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')
def data_entry():
c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
conn.commit()
c.close()
conn.close()
I'm using flask to build a simple web app but for whatever reason the conn.commit() is not committing the data into the database. I know this because when I manually add something to the database the data doesn't change but the ID section increases each time I test it (because its using auto increment). So basically my current table has ID 1, Username test, Password test and the next entry that I inserted manually (after trying to use my application) was ID 5, Username blah, Password blah. Is there any specific reason that the commit isn't working?
EDIT: I had to change cursor = mysql.connect().cursor() to conn.cursor()
#app.route('/add_data/')
def add_tv_to_database():
conn = mysql.connect()
cursor = mysql.connect().cursor()
cursor.execute("INSERT INTO _accounts VALUES (null, 'test','test')")
conn.commit()
return render_template('index.html')
In the fourth line of your code, change it from cursor = mysql.connect().cursor() to cursor = conn.cursor(). This will ensure that the cursor uses the existing connection to database (from the previous line of code), instead of creating a new MySQL connection.