I want to connect to my database and transfer the data, I got from the DHT22. But it won't execute
cur.execute(..).
As you can see I put an print("h") in there, which never gets printed, it only prints the Test.
Would really appreciate your help guys.
import time
import board
import adafruit_dht
import mariadb
import sys
from datetime import datetime
dhtDevice = adafruit_dht.DHT22(board.D17)
conn = mariadb.connect(
user="root",
password="password",
host="localhost",
database="messstation")
cur = conn.cursor()
x = 0
while x < 5:
x = x+1
time.sleep(2.0)
try:
# Print the values to the serial port
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
print('Temp', temperature_c)
print('Humidty:', humidity)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
try:
cur.execute("INSERT INTO messstation (uhrzeit,luftfeuchtigkeit, raumtemperatur) VALUES (?, ?, ?)", (dt_string, temperature_c, humidity))
print("h")
conn.commit()
except:
print("Test")
except:
print("Not working")
pass
conn.close()
I had to put in INSERT INTO root. After that it worked fine.
Thank you for your help!
Related
When I try to import data to sql using python I keep getting string too long exception on a string that is 285 characters long.
I have defined the TweetText-column type in GG as varchar(max) and when I paste the string manually, instead of TweetText.row, and try to import it using this code it works fine which is why i think it is row.TweetText that creates the mistake. Do you have an idea as to why it says string too long?
The code:
import sys
import pypyodbc as odbc
import pandas as pd
import os
import csv
def connect(path):
data = pd.read_csv (path)
data.rename( columns={'Unnamed: 0':'TweetNumber'}, inplace=True )
df = pd.DataFrame(data)
SERVER_NAME = 'DESKTOP-9736NS2\SQLEXPRESS'
DATABASE_NAME = 'DB.Tweets'
DRIVER= 'SQL Server'
conn_string = f"""
Driver={{{DRIVER}}};
Server={{{SERVER_NAME}}};
Database={{{DATABASE_NAME}}};
Trust_Connection=yes;
"""
try:
conn = odbc.connect(conn_string)
except Exception as e:
print(e)
print('task is terminated')
sys.exit()
else:
cursor = conn.cursor()
insert_statement = """
INSERT INTO [GG]
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
"""
try:
toggle =0
for row in df.itertuples():
cursor.execute(insert_statement,(row.TweetNumber, row.UserName, row.RealName, row.FollowerCount, row.TweetCount, row.TweetText, row.TweetLikes, row.TweetRetweets, row.TweetId, row.DatePosted, row.MediaUrl))
if toggle ==0:
print(print(row.UserName))
toggle=1
except Exception as e:
print(e)
cursor.rollback()
print('transaction rolled back')
stop=1
else:
print('records inserted succesfully')
cursor.commit()
cursor.close()
stop=0
finally:
if conn.connected == 1:
print('connection closed')
conn.close()
return(stop)
g=1
stop = 0
for filename in os.scandir('C:/Users/Christian/OneDrive - University of Copenhagen/Documents/UniKU/AProjects/DatabaseProject/TwitterTweetData'):
if stop ==0:
if g == 1:
if filename.is_file():
connect(filename.path)
stop=connect(filename.path)
The SQL datatypes:
Datatypes
I have a piece of code which is taking Windows logs and inserting various pieces of information into an mySQL database. The code is running perfectly with no errors, but does not actually input the data into the table. The table remains blank. I pulled my mySQL syntax from an example with some modification, so I'm not entirely sure what is going wrong. I have a feeling it has either to do with the data types, or some changes I made to the syntax.
import sys
import pymysql
import pymysql.cursors
import win32evtlog # requires pywin32 pre-installed
import win32evtlogutil
import time
server = 'localhost' # name of the target computer to get event logs
logtype = 'System' # 'Application' # 'Security'
hand = win32evtlog.OpenEventLog(server,logtype)
flags =
win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
events = win32evtlog.ReadEventLog(hand, flags,0)
while True:
for event in events:
evt_tp = event.EventType
if evt_tp != (1 or 2 or 8):
eve_cat = str(('Event Category:', event.EventCategory))
eve_timegen = str(('Time Generated:', event.TimeGenerated))
eve_srcnm = str(('Source Name:', event.SourceName))
eve_id = str(('Event ID:', event.EventID))
eve_typ = str(('Event Type:', event.EventType))
data = event.StringInserts
if data:
print ('Event Data:')
for msg in data:
print(msg)
print(type(eve_cat))
print(type(eve_timegen))
print(type(eve_srcnm))
print(type(eve_id))
print(type(eve_typ))
print(type(data))
time.sleep(10)
else:
eve_cat = ('Event Category:', event.EventCategory)
eve_timegen = ('Time Generated:', event.TimeGenerated)
eve_srcnm = ('Source Name:', event.SourceName)
eve_id = ('Event ID:', event.EventID)
eve_typ = ('Event Type:', event.EventType)
data = event.StringInserts
print('There were no errors found')
print(eve_cat)
print(eve_timegen)
print(eve_srcnm)
print(eve_id)
print(eve_typ)
print(data)
time.sleep(10)
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='ptest',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName',
'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `Type` FROM `win_logs` WHERE `Category`=%s"
cursor.execute(sql, ('webmaster#python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
I can be very wrong.
But this is python.
Indentation matter.
Try just:
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `win_logs` (`Category`, `TimeGenerated`, 'SourceName`, 'EventID', 'Type') VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (eve_cat, eve_timegen, eve_srcnm, eve_id, eve_typ))
I guess your cursor is out of with scope
I have a python script below that attempts to make a connection to a sql db. The database was having some issues and I want the script to try to reconnect to the db so I added try / except. Now however I get the error message of "Unable to connect to database". If I take out the try / except statement the script works perfectly. If anyone can help me with getting the try / except statement working I would greatly appreciate it!
Updated code with connection inside of Try
from __future__ import print_function
try:
import psycopg2
except ImportError:
raise ImportError('\n\033[33mpsycopg2 library missing. pip install psycopg2\033[1;m\n')
sys.exit(1)
import re
import sys
import json
import pprint
outfilepath = "crtsh_output/crtsh_flat_file"
DB_HOST = 'crt.sh'
DB_NAME = 'certwatch'
DB_USER = 'guest'
#conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
#cursor = conn.cursor()
def connect_to_db():
filepath = 'forager.txt'
conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
cursor = conn.cursor()
with open(filepath) as fp:
unique_domains = ''
while True:
try:
for cnt, domain_name in enumerate(fp):
print("Line {}: {}".format(cnt, domain_name))
print(domain_name)
domain_name = domain_name.rstrip()
cursor.execute('''SELECT c.id, x509_commonName(c.certificate), x509_issuerName(c.certificate), x509_notBefore(c.certificate), x509_notAfter(c.certificate), x509_issuerName(c.certificate)$
FROM certificate c, certificate_identity ci WHERE
c.id= ci.certificate_id AND ci.name_type = 'dNSName' AND lower(ci.name_value) =
lower(%s) AND x509_notAfter(c.certificate) > statement_timestamp()''', (domain_name,))
unique_domains = cursor.fetchall()
pprint.pprint(unique_domains)
outfilepath = "crtsh1" + ".json"
with open(outfilepath, 'a') as outfile:
outfile.write(json.dumps(unique_domains, sort_keys=True, indent=4, default=str, ensure_ascii = False))
break
except:
pass
#print("\n\033[1;31m[!] Unable to connect to the database\n\033[1;m")
if __name__ == "__main__":
connect_to_db()
You'll want to do a few things:
move this code:
conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
cursor = conn.cursor()
Into your try, that way when it loops if it dies it will remake the connection.
Like this:
try:
conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
cursor = conn.cursor()
for cnt, domain_name in enumerate(fp):
print("Line {}: {}".format(cnt, domain_name))
print(domain_name)
domain_name = domain_name.rstrip()
I would toss a pause between connections. Your DBA might have code to detect too many connections in a period of time, like this:
#at the top of your code
import time
#in your loop add:
time.sleep(1)
Iteration of each record then print by key display nothing
#!/usr/bin/env python
# Print necessary headers.
print("Content-Type: text/html")
print()
import pymysql.cursors
# Connect to the database.
import pymysql
conn = pymysql.connect(db='candidate', user='root',passwd='123',host='localhost')
print ("connect successful!!")
c = conn.cursor()
c.execute("SELECT can_id,can_name,state,total_votes FROM voting")
result_set = c.fetchall()
for row in result_set:
print(row["can_id"])
print(row) works fine
c = conn.cursor(pymysql.cursors.DictCursor)
or
conn = pymysql.connect(db='candidate', user='root', passwd='123', host='localhost', cursorclass=pymysql.cursors.DictCursor)
I propose some changes using the PyMySQL documentation as reference:
#!/usr/bin/env python
# -*- conding: utf-8 -*-
# Print necessary headers.
print("Content-Type: text/html")
import pymysql.cursors
# Connect to the database.
# That is important: the order of connection parameters
# 1st host, 2nd user, ...
conn = pymysql.connect(
host='localhost'
user='root',
password='123',
db='candidate',
cursorclass=pymysql.cursors.DictCursor # <-- declare cursor class
)
print ("connect successful!!")
try:
with conn.cursor() as c:
query = "SELECT can_id, can_name, state, total_votes FROM voting;"
c.execute(query)
with c as pointer:
for row in pointer.fetchall():
print("Name: %s" % row['can_name'])
finally:
conn.close()
What is wrong with my code? Thank you
import os
import os.path
import time
global item_count
#-*- coding: cp936 -*-
import MySQLdb
import MySQLdb.cursors
import threading
import multiprocessing
from time import sleep,ctime
def qucun():
#connect to mysql
conn=MySQLdb.connect(host="localhost",user="root",passwd="caihong")
cursor=conn.cursor()
try:
cursor.execute("""create database if not exists quad""")
except:
print 'Quad is exist'
conn.select_db('quad')
conn=MySQLdb.connect(host="localhost",user="root",passwd="caihong",db="quad")
#get cursor
cursor=conn.cursor()
try:
cursor.execute("""create table if not exists record(fn1 varchar(100),
fn2 varchar(100),fn3 varchar(100),fn4 varchar(100),
fn5 varchar(100),fn6 varchar(100),fn7 varchar(100),fn8 varchar(100))""")
except:
print 'Table record is exist'
loops=['2013071818_1.txt','2013071818_2.txt','2013071818_3.txt','2013071818_4.txt','2013071818_5.txt']
def loop(nloop,filename):
print 'This loop%s start at:'%nloop,ctime()
#connect to quad
conn=MySQLdb.connect(host="localhost",user="root",passwd="caihong",db="quad")
conn.select_db('quad')
#get cursor
cursor=conn.cursor()
newitem=open('C:\\Python27\\caihong\\%s'%filename,'r')
data=[line.strip() for line in newitem.readlines()]
print data
##put data into value
values=['%s'%data[0],'%s'%data[1],'%s'%data[2],'%s'%data[3],'%s'%data[4],
'%s'%data[5],'%s'%data[6],'%s'%data[7]]
cursor.execute("""insert into record values(%s,%s,%s,%s,%s,%s,%s,%s)""",values);
conn.commit()
cursor.close()
sleep(2)
print 'This loop done at',ctime()
if __name__=='__main__':
print 'starting at:',ctime()
threads=[]
nloops=range(len(loops))
pool=multiprocessing.Pool(processes=2)
for i in nloops:
t=pool.apply_async(loop,(i,loops[i]))
pool.close()
pool.join()
if t.successful():
print 'successful'
print 'all Done at:',ctime()
os.system("pause")
qucun()
You are attempting to call locally defined function in async.
You are trying to share an open connection between processes.
First is tricky to implement in 2.7 and second is impossible in any multiprocessing
You have to use separate connection for each process in process pool.
import os
import os.path
import time
global item_count
#-*- coding: cp936 -*-
import MySQLdb
import MySQLdb.cursors
import threading
import multiprocessing
from time import sleep,ctime
CONNECTION = None
def close_connection():
CONNECTION.close()
def get_connection():
global CONNECTION
#If this process pool member launched for a first time - create connection
if CONNECTION is None:
conn = MySQLdb.connect( host="localhost",
user="root",
passwd="caihong")
cursor = conn.cursor()
try:
cursor.execute("""create database if not exists quad""")
except:
print 'Quad is exist'
conn.select_db('quad')
CONNECTION = MySQLdb.connect(host="localhost",
user="root",
passwd="caihong",
db="quad")
cursor = CONNECTION.cursor()
try:
cursor.execute("""create table if not exists record(fn1 varchar(100),
fn2 varchar(100),fn3 varchar(100),fn4 varchar(100),
fn5 varchar(100),fn6 varchar(100),fn7 varchar(100),fn8 varchar(100))""")
except:
print 'Table record is exist'
# we dont need to close connection after each insert.
# insted - register a finalizer once
# so it will be called right before Pool.close()
multiprocessing.util.Finalize(CONNECTION, close_connection, exitpriority=1)
#use existing connection
return CONNECTION
def loop(nloop, filename):
conn = get_connection()
cursor = conn.cursor()
print 'This loop %s start at: %s'%(nloop, ctime())
with open('C:\\Python27\\caihong\\%s'%filename, 'r') as newitem:
data = [line.strip() for line in newitem.readlines()]
# values=['%s'%data[0],'%s'%data[1],'%s'%data[2],'%s'%data[3],'%s'%data[4],
# '%s'%data[5],'%s'%data[6],'%s'%data[7]]
# ^^^ Thats a bad way to stringify list
cursor.execute('insert into record values(%s)', ','.join(data));
conn.commit()
# we dont need to close connection after each insert.
# cursor.close()
print 'This loop done at', ctime()
LOOPS = ['2013071818_1.txt', '2013071818_2.txt', '2013071818_3.txt', '2013071818_4.txt', '2013071818_5.txt']
if __name__=='__main__':
pool = multiprocessing.Pool(processes=2)
results = []
for i, loopfile in enumerate(LOOPS):
results.apply(pool.apply_async(loop, (i, loopfile)))
pool.close()
pool.join()
if all((res.successful() for res in results)):
print 'successful'
print 'all Done at:', ctime()
os.system('pause')