I found put that I open the database at every request.Is there a way to simplify and improve this code to increase the sqlite speed?
name3 = ' '.join(name2)
import sqlite3
id = 0
location = ""
conn = sqlite3.connect("keywords.db")
c = conn.cursor()
c.execute('select * from kmedicals')
records = c.fetchall()
for record in records:
id = record[0]
location = record[15]
if id == name3:
print name3.capitalize(),':' '\n',location
break
sys.exit()
Do not use import in the middle of your program.
Open the database once at the start of your program.
Select only the records you actually need.
Select only the columns you actually need.
Do not use fetchall; read only the records you actually need.
Do not fetch into a temporary variable if you can use the cursor directly.
import sqlite3
# at startup
conn = sqlite3.connect("keywords.db")
def search_location(name2):
name3 = ' '.join(name2)
c = conn.cursor()
c.execute('SELECT location FROM kmedicals WHERE id = ?', (name3,))
for (location,) in c:
print name3.capitalize(),':' '\n',location
break
else:
pass # not found
Related
import mysql.connector
connection = mysql.connector.connect(user="REMOVED",
password="REMOVED",
host="REMOVED",
database="REMOVED")
cur = connection.cursor()
# Latitude - remove letter A
cur.execute("UPDATE tau._inm_exportados_test_csv SET latitud = REPLACE (latitud, 'a=','');")
print("Latitude change remove letter A - executed!")
# Longitude - remove letter A
cur.execute("UPDATE tau._inm_exportados_test_csv SET longitud = REPLACE (longitud, 'a=','');")
print("Longitude change remove letter A - executed!")
# Latitude - MODIFY COLUMN
cur.execute("ALTER TABLE tau._inm_exportados_test_csv MODIFY COLUMN latitud DECIMAL(10,6);")
print("Latitude - MODIFY COLUMN - executed!")
# Longitude - MODIFY COLUMN
cur.execute("ALTER TABLE tau._inm_exportados_test_csv MODIFY COLUMN longitud DECIMAL(10,6);")
print("Longitude - MODIFY COLUMN - executed!")
# Post Code data type change
cur.execute("ALTER TABLE tau._inm_exportados_test_csv MODIFY COLUMN codigo_postal varchar(255);)")
print("Post Code data type change to varchar(255) - executed!")
connection.commit()
cur.close()
connection.close()
I'm trying to make this simple list of statements work without success. What makes it more confusing is that the first four statements work whereas the final one doesn't work even when I comment out the rest! The final statement gets the following reponse:
mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements
The datatype for codigo_postal is int(11) unlike latitud and longitud which are varchar.
I have tried creating new connections, new cursors, new connections AND cursors. I have tried adding multi="True" and combining statements into one operation. I have tried adding multi="True" to each cur.execute() as both the second and third parameter. I have run the statement in Workbench to ensure the statement is valid and it works.
No success with it here though...
You can use commit after you executed DML (Data Manipulation Language) commands. Also using multi=True can be more convenient to complete this job, but you need to run the generator which created by execute. doc.
Ordinary method:
cur = connection.cursor()
def alter(state,msg):
try:
cur.execute(state)
connection.commit()
except Exception as e:
connection.rollback()
raise e
print(msg)
alter("ALTER TABLE address MODIFY COLUMN id int(15);","done")
alter("ALTER TABLE address MODIFY COLUMN email varchar(35);","done")
alter("ALTER TABLE address MODIFY COLUMN person_id int(35);","done")
With multi=True:
cur = connection.cursor()
def alter(state,msg):
result = cur.execute(state,multi=True)
result.send(None)
print(msg,result)
try:
alter("ALTER TABLE address MODIFY COLUMN id int(45)","done")
alter("ALTER TABLE address MODIFY COLUMN email varchar(25)","done")
alter("ALTER TABLE address MODIFY COLUMN person_id int(25);","done")
connection.commit()
except Exception as e:
connection.rollback()
raise e
I had the same problem.
I wanted my code to be clean and I wanted to have all my commands in a list and just run them in a sequence.
I found this link and this link and finally was able to write this code:
import mysql.connector as sql
from mysql.connector import Error
commands = [
'''
USE sakila;
SELECT * FROM actor;
''',
'''
USE sakila;
SELECT * FROM actor WHERE actor_id < 10;
'''
]
connection_config_dict = {
'user': 'username',
'password': 'password',
'host': '127.0.0.1',
}
try:
connection = sql.connect(**connection_config_dict)
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info, '\n')
cursor = connection.cursor()
for command in commands:
for result in cursor.execute(command, multi=True):
if result.with_rows:
print("Rows produced by statement '{}':".format(
result.statement))
print(result.fetchall())
else:
print("Number of rows affected by statement '{}': {}".format(
result.statement, result.rowcount), '\n')
record = cursor.fetchall()
except Error as e:
print("Error while connecting to MySQL", e, '\n')
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed", '\n')
I would like to read row by row in python from Oracle Select query. I have to build a logic based on the data I am getting from Oracle for specific columns.
I am using cx_Oracle.connect
dsnStr = cx_Oracle.makedsn("xxxx.net", "6000", "XVTRR") # Dev envrionment
con = cx_Oracle.connect(user="SCOTT", password="TIGER", dsn=dsnStr)
print con.version
cursor = con.cursor()
cursor.execute("select * from user_tables where rownum<=1 order by TABLE_NAME")
rows = cursor.fetchall()
col_names=[]
for i in range(0, len(cursor.description)):
col_names.append(cursor.description[i][0])
pp = pprint.PrettyPrinter(width=1024)
pp.pprint(col_names)
pp.pprint(rows)
cursor.close()
con.close()
Please help me.
I'm a newbie to python and i am trying to insert some data in to a mysql table .Seems the query executed with out any issues, however i don't see any record added on to the table.
Any help would be greatly appreciated.
Cheers,
Aditya
connection = mysql.connector.connect(user='sandboxbeta2503', password='XXX',
host='myreleasebox.com',
database='iaas')
print ("Updating the history in bulk_notification_history")
cursor = connection.cursor()
timestamp = time.strftime("%Y-%m-%d %X")
notification_type = "Notify Inactive users"
usercount= 45
query = ("INSERT INTO iaas.bulk_notification_history"
"(nty_date,notification_type,user_count)"
"VALUES (%s,%s,%s)")
data = (time.strftime('%Y-%m-%d %H:%M:%S'),notification_type, usercount)
linker1 = cursor.execute(query,data)
print (linker1)
cursor.close()
connection.close()
My code:
import sqlite3
conn = sqlite3.connect("keywords.db")
def search_location(name2):
name3 = ' '.join(name2)
c = conn.cursor()
c.execute('SELECT location FROM INPUT WHERE id = ?', (name3,))
for (location,) in c:
print name3.capitalize(),':' '\n',location
break
else:
pass # not found
Sqlite table:
sqlite>
CREATE TABLE input(
ID INT PRIMARY KEY AUTOINCRREMENT, NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY ....
);
I keep getting the error: No column LOCATION found. Items in name3 are to match the items in column NAME. If I change the code to read :
'SELECT NAME FROM INPUT WHERE id = ?', (name3,)
The error is the same. Please refer to the question I had asked earlier on How to improve sqlite database request and speed
I have written up the following initializing routine for an sqlite3 db table, but something tells me this is a) fragile, and/or b) Just a Bad Idea(tm).
The notion is that if the table is not present or it has not been initiailized, the try blocks will fault and the data will be created. In initial testing this works - although I am not seeing the defaults printed to console when the script runs: I get an empty tuple printed. Examining the database using the sqlite shell I see the data is present.
But that niggling feeling lingers that there is something very wrong with this approach. Thoughts? Opinions? Advice?
import sqlite3 as lite
import sys
def insert_defaults():
conn = lite.connect('db.sqlite')
with conn:
cur = conn.cursor()
defaults = (
('mykey','value for key one'),
('anotherkey','value for key two')
)
cur.executemany("INSERT INTO Settings(key,value) VALUES ( ?, ? )", defaults)
def initialize():
conn = lite.connect('db.sqlite')
settings = ()
try:
conn.row_factory = lite.Row
cur = conn.cursor()
cur.execute("SELECT * FROM Settings")
if cur.rowcount < 1:
insert_defaults()
cur.execute("SELECT * FROM Settings")
settings = cur.fetchall()
except lite.Error, e:
print "Error: %s" % e.args[0]
print "Trying to create missing table"
try:
cur.execute( "DROP TABLE IF EXISTS Settings" )
cur.execute("CREATE TABLE IF NOT EXISTS Settings (id INTEGER PRIMARY KEY, key TEXT NOT NULL, value TEXT)")
insert_defaults()
except lite.Error, e:
if conn:
conn.rollback()
print "Error: %s" % e.args[0]
sys.exit(1)
finally:
if conn:
conn.close()
return settings
if __name__ == "__main__":
print initialize()
Erik
Relying on exceptions to detect that the table does not exist is not very reliable because there could be other errors that have nothing to do with what you want to check.
The easiest way to ensure that the table is created is to just execute CREATE TABLE IF NOT EXISTS ... when your program is started; this will be ignored if the table already exists.
To check for some records existing, using a SELECT is fine.
However, if you have a UNIQUE or PRIMARY KEY constraint on the key column, you could just execute INSERT OR IGNORE INTO Settings....
You should not use a separate connection in insert_defaults(); this will lead to problems if you don't get your transaction commits correct.