I want to insert a record into usage_information while getting the machine_id from machines tables
How the query should be?
I'm using something like this for selecting from both tables
cur.execute(
'''SELECT U.timestamp, M.hostname, M.up_time,
U.cpu_utilization, U.running_processes,
U.total_memory, U.available_memory,
U.used_memory, U.total_disk_capacity,
U.free_disk_space, U.used_disk_space,
U.network_requests_sent,
U.network_requests_received
FROM usage_information U, machines M
WHERE U.machine_id = M.machine_id
'''
)
Related
I am trying to insert data into a table created in oracle sql using python. It temporarily inserts data into the table but as soon as the python process ends , the data is deleted.
def submit_button(roll_no,name,marks):
sql_query = 'INSERT INTO assignment_7 VALUES (:r,:n,:m)'
c.execute(sql_query,[int(roll_no),name,int(marks)])
c.execute('SELECT * FROM assignment_7')
for rows in c:
print(rows[0],'-',rows[1],'-',rows[2])
For example if (12,'aryan',20) are inserted into the table , the print statement works , but actually no data gets inserted when I check the table itself .
Name Null? Type
ROLL_NO NOT NULL NUMBER
STUDENT_NAME VARCHAR2(30)
MARKS NUMBER
try to add c.commit
def submit_button(roll_no,name,marks):
sql_query = 'INSERT INTO assignment_7 VALUES (:r,:n,:m)'
c.execute(sql_query,[int(roll_no),name,int(marks)])
c.commit
c.execute('SELECT * FROM assignment_7')
for rows in c:
print(rows[0],'-',rows[1],'-',rows[2])
I have a sqlite database named StudentDB which has 3 columns Roll number, Name, Marks. Now I want to fetch only the columns that user selects in the IDE. User can select one column or two or all the three. How can I alter the query accordingly using Python?
I tried:
import sqlite3
sel={"Roll Number":12}
query = 'select * from StudentDB Where({seq})'.format(seq=','.join(['?']*len(sel))),[i for k,i in sel.items()]
con = sqlite3.connect(database)
cur = con.cursor()
cur.execute(query)
all_data = cur.fetchall()
all_data
I am getting:
operation parameter must be str
You should control the text of the query. The where clause shall allways be in the form WHERE colname=value [AND colname2=...] or (better) WHERE colname=? [AND ...] if you want to build a parameterized query.
So you want:
query = 'select * from StudentDB Where ' + ' AND '.join('"{}"=?'.format(col)
for col in sel.keys())
...
cur.execute(query, tuple(sel.values()))
In your code, the query is now a tuple instead of str and that is why the error.
I assume you want to execute a query like below -
select * from StudentDB Where "Roll number"=?
Then you can change the sql query like this (assuming you want and and not or) -
query = "select * from StudentDB Where {seq}".format(seq=" and ".join('"{}"=?'.format(k) for k in sel.keys()))
and execute the query like -
cur.execute(query, tuple(sel.values()))
Please make sure in your code the provided database is defined and contains the database name and studentDB is indeed the table name and not database name.
I am pulling data from a MSSQL db using pyodbc which returns my data set in a list. This data then needs to be transferred into a MySQL db. I have written the following stored procedure in MySQL.
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_int_pmt`(
IN pmtamt DECIMAL(16,10),
IN pmtdt DATETIME,
IN propmtref VARCHAR(128),
IN rtdinv_id INT(11)
)
BEGIN
INSERT INTO ay_financials.payment
(
pmtamt,
pmtdt,
propmtref,
rtdinv_id
)
VALUES
(
pmtamt,
pmtdt,
propmtref,
rtdinv_id
);
END
The procedure works fine if I am inserting one record at the time. So, for now, I am iterating over the list from my MSSQL query and call the procedure for each record. I am using this code:
cursor = cnxn.cursor()
cursor.execute(""" SELECT *
FROM [%s].[dbo].[pmt]
WHERE pmtdt BETWEEN '2018-01-01' AND '2018-12-31'""" %(database))
a = cursor.fetchmany(25)
cnxn.close()
import pymysql
# MySQL configurations
un = 'ssssssss'
pw = '****************'
db = 'ay_fnls'
h = '100.100.100.100'
conn = pymysql.connect(host=h, user=un, password=pw, db=db, cursorclass=pymysql.cursors.DictCursor)
cur = conn.cursor()
for ay in a:
cur.callproc('sp_int_pmt',(ay.pmtamt,ay.pmtdt,ay.propmtref,ay.rtdinv_id))
conn.commit()
The problem I will have in production is this list will contain 10,000-100,000 every day. Iterating over that data doesn't seem like an optimized way to handle this.
How can I use the full list from the MSSQL query, call the MySQL procedure one time and insert all the relevant data?
How can I use the full list from the MSSQL query, call the MySQL procedure one time and insert all the relevant data?
You can't do that with your stored procedure as written. It will only insert one row at a time, so to insert n rows you would have to call it n times.
Also, as far as I know you can't modify the stored procedure to insert n rows without using a temporary table or some other workaround because MySQL does not support table-valued parameters to stored procedures.
You can, however, insert multiple rows at once if you use a regular INSERT statement and .executemany. pymysql will bundle the inserts into one or more multi-row inserts
mssql_crsr = mssql_cnxn.cursor()
mssql_stmt = """\
SELECT 1 AS id, N'Alfa' AS txt
UNION ALL
SELECT 2 AS id, N'Bravo' AS txt
UNION ALL
SELECT 3 AS id, N'Charlie' AS txt
"""
mssql_crsr.execute(mssql_stmt)
mssql_rows = []
while True:
row = mssql_crsr.fetchone()
if row:
mssql_rows.append(tuple(row))
else:
break
mysql_cnxn = pymysql.connect(host='localhost', port=3307,
user='root', password='_whatever_',
db='mydb', autocommit=True)
mysql_crsr = mysql_cnxn.cursor()
mysql_stmt = "INSERT INTO stuff (id, txt) VALUES (%s, %s)"
mysql_crsr.executemany(mysql_stmt, mssql_rows)
The above code produces the following in the MySQL general_log
190430 10:00:53 4 Connect root#localhost on mydb
4 Query INSERT INTO stuff (id, txt) VALUES (1, 'Alfa'),(2, 'Bravo'),(3, 'Charlie')
4 Quit
Note that pymysql cannot bundle calls to a stored procedure in the same way, so if you were to use
mysql_stmt = "CALL stuff_one(%s, %s)"
instead of a regular INSERT then the general_log would contain
190430 9:47:10 3 Connect root#localhost on mydb
3 Query CALL stuff_one(1, 'Alfa')
3 Query CALL stuff_one(2, 'Bravo')
3 Query CALL stuff_one(3, 'Charlie')
3 Quit
I want to execute an SQL script against a MSSQL server. In the script I have created table variable by selecting data from a table, then get the data from the variable. But it returns an array with no values.
SQL:
DECLARE #ProductTotals TABLE
(
ProductID int,
Revenue money
)
INSERT INTO #ProductTotals (ProductID, Revenue)
SELECT ProductID, SUM(UnitPrice * Quantity)
FROM [Order Details]
SELECT * FROM #ProductTotals
Python:
data = connection_to_database.execute(sqlFile)
for i in data:
print(i)
Good, what happens is that I have two tables in the same database, the first table I will call patient, the second appointment .... both have the same column that is "cc" .... I look for a date, That match my table in quotes and grab the "cc", then go to the patient table and bring the name in such a way that I print name + cc + date ...... what worries me is how I make that link Between the tables with python, attached images to see the database and part of the code of which I try to join and print the matches of "cc".
Thank you for your cooperation.
Data from the first table
Data from the second table
You didn't mention what library you are/intend to use for MySQL. I will assume pymssql. Here is a simple example to get you started based off their documentation and Hatik's query.
import pymssql
conn = pymssql.connect("localhost", "admin", "password", "database")
cursor = conn.cursor()
cursor.execute("""
SELECT B.NAME, B.CC, C.DATE FROM
APPOINTMENT A LEFT JOIN PATIENT B ON A.CC = B.CC
""")
row = cursor.fetchone()
while row:
print row
row = cursor.fetchone()
conn.close()