How to deal with configuration variables in Python? - python

I'm new to Python and I'm doing some tests with it.
What I need to know is what is the best way of dealing with configuration variables.
For example, for this code:
import twitter
import random
import sqlite3
import time
import bitly_api #https://github.com/bitly/bitly-api-python
class TwitterC:
def logtodatabase(self, tweet, timestamp):
# Will log to the database
database = sqlite3.connect('database.db') # Create a database file
cursor = database.cursor() # Create a cursor
cursor.execute("CREATE TABLE IF NOT EXISTS twitter(id_tweet INTEGER AUTO_INCREMENT PRIMARY KEY, tweet TEXT, timestamp TEXT);") # Make a table
# Assign the values for the insert into
msg_ins = tweet
timestamp_ins = timestamp
values = [msg_ins, timestamp_ins]
# Insert data into the table
cursor.execute("INSERT INTO twitter(tweet, timestamp) VALUES(?, ?)", values)
database.commit() # Save our changes
database.close() # Close the connection to the database
In this code, how can I replace 'database.db' with a variable outside of the class, for a better configuration. ?
Best Regards,

Or you could use argparse if you want to pass in configuration from the command line.
Then when creating the TwitterC class, you could pass in the configuration options you want.
class TwitterC:
def __init__(self, database):
self.database = database
def logtodatabase(self, tweet, timestamp):
# Will log to the database
database = sqlite3.connect(self.database) # Create a database file
#(...)

You could use ConfigParser out of the Python Standard Library.

You can create a python script that include the configuration variables:
config.py:
dbname = 'database.db'
...
your file:
import config
database = sqlite3.connect(config.dbname)

Related

Insert Variables With Python And Tkinter, Into Sqlite3 Table

Sorry this is my first post.
I try to insert into sqlite3 table datas that i get using tkinter entries (python)
but i always obtain empty fields in the table.my code:
import sqlite3
from tkinter import *
def data_entry():
CDB.execute('insert into COSTUMERS (NAME,CODE)values(?,?)', (NAME_E,CODE_E))
DB.commit()
CDB.close()
DB.close()
X=Tk()
NAME=StringVar()
CODE=StringVar()
DB=sqlite3.connect('DB.db')
CDB=DB.cursor()
CDB.execute('''create table if not exists COSTUMERS
(ID integer primary key autoincrement,
NAME text(20), CODE text(10))''')
NAME_E=Entry(X,textvariable=NAME).pack()
CODE_E=Entry(X,textvariable=CODE).pack()
SAVE=Button(X,text='SAVE',command=data_entry).pack()
X.mainloop()
I think you should refactoring your code.
First of all use a naming convention on sql commands, that is, the uppercase
commands and the rest in lowercase.
This also for what concerns the code, see pep 8
I modified your script, in sqlite you don’t need to declare an autoincrement field
if you declare it as primary key.
I did not close the cursor and the database to insert more records as you can see
from the attached images.
And you don’t even need to declare Entry if you use textvariable, you can use
these directly to pass the values.
#!/usr/bin/python3
import tkinter as tk
import sqlite3 as lite
def data_entry():
sql = "INSERT INTO customers (customer,code)VALUES(?,?)"
args = (customer.get(),code.get())
print(sql, args)
cur.execute(sql, args)
dbms.commit()
sql = "SELECT * FROM customers"
cur.execute(sql)
rs = cur.fetchall()
for i in rs:
print(i)
#cur.close()
#dbms.close()
tk.X=tk.Tk()
customer = tk.StringVar()
code = tk.StringVar()
dbms = lite.connect('DB.db')
cur = dbms.cursor()
sql = "CREATE TABLE IF NOT EXISTS customers (customer_id INTEGER PRIMARY KEY, customer TEXT, code TEXT);"
cur.execute(sql)
tk.Entry(tk.X,textvariable=customer).pack()
tk.Entry(tk.X,textvariable=code).pack()
tk.Button(tk.X, text="Save", command=data_entry).pack()
tk.X.mainloop()

Python SQLite insert statement executing but not inserting any data

Dear Stackoverflow community,
i have the following problem. I am using Python 3.5
and SQLite3 package and I'm trying to insert 20 cities into the city table of my database. The problem ist that after executing the code there is no error message but the entries won't show up in the database.
My Code looks like this
Database.py: this file encapsulates the database and provides the functionality to easily execute queries.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3
from sqlite3 import Error
class Database:
database_connection = 0;
def __init__(self, path_to_database):
self.__try_connect__(path_to_database)
def __try_connect__ (self, path_to_database):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
try:
self.database_connection = sqlite3.connect(path_to_database)
except Error as e:
print(e)
def execute_query(self, query):
database_cursor = self.database_connection.cursor()
print(database_cursor.execute(query))
def insert(self, table_name, assoziative_key_value_array):
print("insert not yet implemented")
DatabaseCreator.py: if you want to try to run my code you can use
this class that creates the database for you
from Database import Database
class DatabaseCreator:
database = 0
#staticmethod
def create(path_to_database):
database = Database(path_to_database)
DatabaseCreator.delete_old(database)
DatabaseCreator.create_new(database)
#staticmethod
def delete_old(database):
print("#DatabaseCreator - delete old")
database.execute_query("DROP TABLE 'Städte'")
database.execute_query("DROP TABLE 'Vereine'")
database.execute_query("DROP TABLE 'Präsidenten'")
database.execute_query("DROP TABLE 'Spieler'")
#staticmethod
def create_new(database):
print("#DatabaseCreator - create new");
database.execute_query("CREATE TABLE 'Städte' ('Id' INTEGER PRIMARY KEY, 'Name' Varchar NOT NULL);")
database.execute_query("CREATE TABLE 'Präsidenten' ('Id' INTEGER PRIMARY KEY, 'Name' Varchar NOT NULL);")
database.execute_query("CREATE TABLE 'Vereine' ('Id' INTEGER PRIMARY KEY, 'Name' Varchar NOT NULL, 'Tabellenplatz' INTEGER NOT NULL, 'PräsidentId' INTEGER NOT NULL, 'StadtId' INTEGER NOT NULL, FOREIGN KEY('StadtId') REFERENCES 'Städte'('Id'), FOREIGN KEY('PräsidentId') REFERENCES 'Präsidenten'('Id') ,CONSTRAINT exclusive_präsidentschaft UNIQUE ('PräsidentId'));")
database.execute_query("CREATE TABLE 'Spieler' ('Id' INTEGER PRIMARY KEY, 'Name' Varchar NOT NULL, 'Alter' INTEGER NOT NULL, 'Position' Varchar NOT NULL, 'VereinId' INTEGER NOT NULL, FOREIGN KEY('VereinId') REFERENCES 'Vereine'('Id'));")
DatabaseSeeder.py: this class uses the Database class within its static methods and tries to insert
the 20 cities into the database.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Database import Database
class DatabaseSeeder:
database = 0
#staticmethod
def seed(database):
DatabaseSeeder.seed_cities(database)
DatabaseSeeder.seed_presidents(database)
DatabaseSeeder.seed_clubs(database)
DatabaseSeeder.seed_players(database)
#staticmethod
def seed_cities(database):
print("#DatabaseSeeder - seed cities")
cities = [
"Berlin",
"Hamburg",
"München",
"Köln",
"Frankfurt am Main",
"Stuttgart",
"Düsseldorf",
"Dortmund",
"Essen",
"Leipzig",
"Bremen",
"Dresden",
"Hannover",
"Nürnberg",
"Duisburg",
"Bochum",
"Wuppertal",
"Bielefeld",
"Bonn",
"Münster"
]
insert_city_sql_template = "INSERT INTO 'Städte' ('{0}') VALUES ('{1}');"
for city in cities:
sql_query = insert_city_sql_template.format("Name", city)
print(sql_query)
database.execute_query(sql_query)
My template for createing the final sql statement looks like this
insert_city_sql_template = "INSERT INTO 'Städte' ('{0}') VALUES ('{1}');"
It gets formatted into the final sql statement here
sql_query = insert_city_sql_template.format("Name", city)
When I print the sql statement before it is executed it looks like this
INSERT INTO 'Städte' ('Name') VALUES ('Berlin');
Application.py: in my main class I create a new database connection at first and hand this connection over to the DatabaseSeeder class.
from tkinter import *
import sqlite3
from sqlite3 import Error
from Database import Database
from table import Table
from DatabaseCreator import DatabaseCreator
from DatabaseSeeder import DatabaseSeeder
def main():
database_path = "./database.db"
DatabaseCreator.create(database_path)
database = Database(database_path)
DatabaseSeeder.seed(database)
When I execute my code I don't get any error message
but the entries just won't show up in the database.
When I copy the SQL statement from my code and directly execute it on the sqlite cli it works perfectly fine.
I hope someone of you knows what I am missing. Thanks a lot :)
Okay I found a solution thanks to #stovfl. I really was missing the commit statement. Now my execute_query method looks like this with a commit at the end.
def execute_query(self, query):
try:
database_cursor = self.database_connection.cursor()
database_cursor.execute(query)
except sqlite3.IntegrityError as e:
print('sqlite error: ', e.args[0]) # column name is not unique
self.database_connection.commit()
Thanks a lot for your help and always remember to commit your changes :D
Greetings

sqlite3 cursor.fetchall() returns an empty array

I'm ssh'd into a remote server, and there I created two python files as a test: one to create a database, and one to read data from it.
The script that creates the database
import os
import sqlite3
# set up database
conn = sqlite3.connect('Materials.db')
c = conn.cursor()
def createTable():
c.execute("DROP TABLE IF EXISTS images")
c.execute("CREATE TABLE images(ID TEXT, url TEXT)")
createTable()
path = os.getcwd()
imagepath = "/home/rootadmin/1080_images"
imagedir = os.listdir(imagepath)
for image in range(0,len(imagedir)):
c.execute('INSERT INTO images(ID, url) VALUES(?,?)',(imagedir[image],'www.google.com'))
print(imagedir[image])
Here the print commands the data that is required, e.g. it prins the ID's of the images.
In my script to read the data from the db:
import sqlite3
conn = sqlite3.connect('Materials.db')
c = conn.cursor()
c.execute('SELECT ID FROM images')
objectId = c.fetchall()
print(objectId)
I have a limited knowledge of sqlite3, but I would expect the print command in the second script to print the ID's found in the images table, from the Materials.db, yet it returns an empty array.
You need to commit your transaction when inserting:
for image in range(0,len(imagedir)):
c.execute('INSERT INTO images(ID, url) VALUES(?,?)',(imagedir[image],'www.google.com'))
print(imagedir[image])
conn.commit()
or use the connection as a context manager to auto-commit when the context exits:
with conn:
for image in range(0,len(imagedir)):
c.execute('INSERT INTO images(ID, url) VALUES(?,?)',(imagedir[image],'www.google.com'))
print(imagedir[image])
This also ensures that the transaction is explicitly rolled back if there was an exception.

Access sqlite database from 2 python files

I have been trying to create a sqlite database using one python file and access data from it using another, but keep getting an error. I have 2 files, main.py and file2.py
main.py:
import sqlite3, os
conn = sqlite3.connect(':memory:')
queryCurs = conn.cursor()
def createTable():
queryCurs.execute('''CREATE TABLE test(id INTEGER PRIMARY KEY, name TEXT)''')
def addInitial(name):
queryCurs.execute('''INSERT INTO test(name) VALUES (?)''',(name,))
createTable()
addInitial("John")
conn.commit()
os.system('file2.py')
and here is the code in file2.py
import sqlite3, os, time
conn = sqlite3.connect(':memory:')
queryCurs = conn.cursor()
queryCurs.execute('SELECT name FROM test WHERE id=1')
for i in queryCurs:
for j in i:
name = j
print name
conn.commit()
I receive the error: OperationalError: no such table: test
Each connect call creates its own in-memory database.
To share the same in-memory database, create a single connection and share that Python object in both modules.

Add new field to access table using python

I have an access table that I am trying to add fields programmatically using Python. It is not a personal geodatabase. Just a standard Access database with some tables in it.
I have been able to access the table and get the list of field names and data types.
How do I add a new field and assign the data type to this Access table using Python.
Thanks!
SRP
Using the pyodbc module:
import pyodbc
MDB = 'c:/path/to/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'my_password'
conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
c = conn.cursor()
c.execute("ALTER TABLE my_table ADD COLUMN my_column INTEGER;")
conn.commit()
c.close()
conn.close()
Edit:
Using win32com.client...
import win32com.client
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=c:/path/to/my.mdb;'
conn.Open(DSN)
conn.Execute("ALTER TABLE my_table ADD COLUMN my_column INTEGER;")
conn.Close()

Categories

Resources