I am new in mysql, I have a table with two cols tag_id and time_stamp, I use a python connector. I need to insert new tag_id just only if did not insert the same tag_id in last 5 min (or some other duration). How can I do that using python mysql.connector?
create table:
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='test',
user='root',
password='root')
mySql_Create_Table_Query = """CREATE TABLE tags (
Id int(11) NOT NULL AUTO_INCREMENT,
tag_id varchar(250) NOT NULL,
time_stamp Datetime NOT NULL,
PRIMARY KEY (Id)) """
cursor = connection.cursor()
result = cursor.execute(mySql_Create_Table_Query)
print("Laptop Table created successfully ")
except mysql.connector.Error as error:
print("Failed to create table in MySQL: {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
Insertion function:
def insertVariblesIntoTable(tag, time_stamp):
try:
connection = mysql.connector.connect(host='localhost',
database='test',
user='root',
password='root')
cursor = connection.cursor()
mySql_insert_query = """INSERT INTO tags(tag_id, time_stamp )
VALUES (%s, %s) """
recordTuple = (tag, time_stamp)
cursor.execute(mySql_insert_query, recordTuple)
connection.commit()
print("Record inserted successfully into tags table")
except mysql.connector.Error as error:
print("Failed to insert into MySQL table {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
I'd just do a query of the table like this:
import arrow
check = '''select max(timestamp) from tags where tag_id = {}'''
try:
with conn.cursor() as curs:
curs.execute(check.format(tag_id))
max_time = curs.fetchone()
if max_time <= arrow.utcnow().shift(minutes=-5).format('YYYY-MM-DD HH:mm:ss'):
#run the inserts
else:
pass
Related
i'm taking data from textfile which contains some duplicate data.And i'm trying to insert them into database without duplicating.i'm in trouble where inserting duplicate data.it should not be inserted again.data are not static values.
text_file = open(min_file, "r")
#doc = text_file.readlines()
for line in text_file:
field = line.split(";")
print(field)
try:
connection = mysql.connector.connect(host='localhost',
database='testing',
user='root',
password='root')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
mycursor = connection.cursor()
#before inserting
mycursor.execute("Select * from ftp")
myresult = mycursor.fetchall()
for i in myresult:
print(i)
sql ="Insert into ftp(a,b,c,d) \
select * from( Select VALUES(%s,%s,%s,%s) as temp \
where not exists \
(Select a from ftp where a = %s) LIMIT 1"
mycursor.execute(sql,field)
print(mycursor.rowcount, "record inserted.")
connection.commit()
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
One option is to add Unique constraint and let the DB validate uniqueness, this will throw exception which you can catch and skip.
Why i can't save a array list to mysql DB ^^.
This Script work
Code work:
########################################
# Importing modules
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="*******",
password="*********",
database="meineTestDB",
)
cursor = conn.cursor()
insert_stmt = (
"INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME)"
"VALUES (%s, %s)"
)
data = ('Test1', 'Test2')
try:
# Executing the SQL command
cursor.execute(insert_stmt, data)
# Commit your changes in the database
conn.commit()
except:
# Rolling back in case of error
conn.rollback()
print("Data inserted")
#Closing the connection
conn.close()
########################################
but i want save array to MySQL DB
I try | data = (cars1, cars2) | or | data = ((cars1), (cars2)) | but it doesn't work.
########################################
# Importing modules
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="*******",
password="*********",
database="meineTestDB",
)
cars1 = ["Ford", "Volvo", "BMW"]
cars2 = ["Ford", "Volvo", "BMW"]
cursor = conn.cursor()
insert_stmt = (
"INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME)"
"VALUES (%s, %s)"
)
data = (cars1, cars2)
try:
# Executing the SQL command
cursor.execute(insert_stmt, data)
# Commit your changes in the database
conn.commit()
except:
# Rolling back in case of error
conn.rollback()
print("Data inserted")
#Closing the connection
conn.close()
########################################
sry for this Symple question i'm new to that Space.
I hope you can help my.
THX for all answer.
It looks like your post is mostly code; please add some more details. 0.o
So im trying to insert values into a MYSQL database table but the following error keeps on popping up.
would really appreciate some help.
This is my code which i wrote to input a value from a file and store it in a database table.
import mysql.connector
import pickle
try:
connection = mysql.connector.connect(host='localhost',
database='PETROL',
user='Sarthak',
password='q1w2e3r4t5')
cursor = connection.cursor ( )
print(connection)
fp1 = open ("D:/Python/petrol/pdate/pdate.txt" , "rb+")
while True :
try :
pdate = pickle.load (fp1)
cursor.execute("DROP TABLE IF EXISTS DATES")
cursor.execute("CREATE TABLE DATES (ID INT AUTO_INCREMENT PRIMARY KEY,Date DATE)")
cursor.execute ("INSERT INTO DATES(Date) VALUES(pdate)")
cursor.execute("SHOW TABLES")
cursor.commit()
except EOFError :
fp6.close ()
except mysql.connector.Error as error:
print("Failed to create table in MySQL: {}".format(error))
cursor.close()
connection.close()
The following error keeps on popping up -:
Failed to create table in MySQL: 1054 (42S22): Unknown column 'pdate' in 'field list'
I am not able to encounter what problem is caused by the insert statement which i wrote.
Its better you write your insert statement like this
query = "INSERT INTO DATES(`Date`) VALUES(%s)" cursor.execute (query, pdate)
import pickle
try:
connection = mysql.connector.connect(host='localhost',
database='PETROL',
user='Sarthak',
password='q1w2e3r4t5')
cursor = connection.cursor ( )
print(connection)
fp1 = open ("D:/Python/petrol/pdate/pdate.txt" , "rb+")
while True :
try :
pdate = pickle.load (fp1)
cursor.execute("DROP TABLE IF EXISTS DATES")
cursor.execute("CREATE TABLE DATES (ID INT AUTO_INCREMENT PRIMARY KEY,Date DATE)")
query = "INSERT INTO DATES(`Date`) VALUES(%s)"
cursor.execute (query, pdate)
cursor.execute("SHOW TABLES")
connection.commit() #use connection.commit instead of cursor.commit
except EOFError :
fp6.close ()
except mysql.connector.Error as error:
print("Failed to create table in MySQL: {}".format(error))
cursor.close()
connection.close()```
When I executing the following function, I get the following response:
Failed to insert data into sqlite table: no such table: users
user_id = uuid.uuid4()
save_record.name = name
save_record.score = score
last_time = datetime.now()
try:
conn = sqlite3.connect('OnlineJeopardy.db')
cursor = conn.cursor()
print("Successfully Connected to OnlineJeopardy DB.")
sqlite_insert_query = """INSERT INTO users
(User_id, Name, Score, Last_time)
VALUES
(?, ?, ?, ?)"""
add_to_db = cursor.execute(sqlite_insert_query, (user_id, name, score, last_time))
conn.commit()
cursor.close()
except sqlite3.Error as error:
print("Failed to insert data into sqlite table: ", error)
finally:
if (conn):
conn.close()
print("The SQLite connection is closed")
When I execute this query in DB Browser, with actual values instead of placeholders, it all goes well.
I've tried swapping placeholders to actual values (as below) within the query in sqlite3 but the outcome was the same.
conn = sqlite3.connect('OnlineJeopardy.db')
cursor = conn.cursor()
print("Successfully Connected to OnlineJeopardy DB.")
sqlite_insert_query = """INSERT INTO users
(User_id, Name, Score, Last_time)
VALUES
('ID-1337', 'Adam', 20, '2020-06-12 23:18:58')"""
add_to_db = cursor.execute(sqlite_insert_query)
conn.commit()
cursor.close()
I'm writing unit tests to test my environment.
I have created tests such as:
def test_database_file_present_and_readable(self):
self.assertTrue(os.access(path_db_file, os.R_OK))
def test_connect_to_db(self):
conn = sqlite3.connect(path_db_file)
conn.close()
def test_create_table(self):
conn = sqlite3.connect(path_db_file)
cur = conn.cursor()
cur.execute("CREATE TABLE test_table (id integer PRIMARY KEY, name text)")
conn.commit()
conn.close()
def test_insert_into_table(self):
conn = sqlite3.connect(path_db_file)
cur = conn.cursor()
cur.execute("insert into test_table (name) values (?)", ["Test value"])
conn.commit()
conn.close()
def test_update_table(self):
conn = sqlite3.connect(path_db_file)
cur = conn.cursor()
cur.execute("update test_table set id = 2 where id = ?", [1])
conn.commit()
conn.close()
def test_delete_from_table(self):
conn = sqlite3.connect(path_db_file)
cur = conn.cursor()
cur.execute("delete from test_table where id = ?", [2])
conn.commit()
conn.close()
def test_if_test_table_is_empty(self):
conn = sqlite3.connect(path_db_file)
cur = conn.cursor()
result = cur.execute("select exists(select 1 from test_table)").fetchall()
conn.commit()
conn.close()
self.assertTrue(result == 1)
def test_delete_table(self):
conn = sqlite3.connect(path_db_file)
cur = conn.cursor()
cur.execute("drop table test_table")
conn.commit()
conn.close()
And during program execution order of tests is unknown - how to set the order or how to clean up database after creating tests with table creation?
You can get pointers about test method execution order here: Python unittest.TestCase execution order
One suggestion - if you are going for such testing, it's better to mock external dependencies like sqlite & test only the code you've written.