update col values using python list mysql - python

I read a column's length in the code given below. Then, I have a list fakeNames whose length is equal to the column's original length.
How can I replace the values of the original column person_nameswith my list fakeNamesvalues? The changes should be reflected in the database.
import MySQLdb
# Connect
db = MySQLdb.connect(
host="abc.us-east-1.rds.amazonaws.com",
user="abc",
password="password",
)
cursor = db.cursor()
# Execute SQL select statement
cursor.execute("SELECT COUNT(person_names) FROM z3_table")
number_of_names = (cursor.fetchone()[0])
print(number_of_names)
fakeNames = []
# Commit your changes if writing
# In this case, we are only reading data
# db.commit()
# Close the connection
db.close()
This means that each value from the list fakeNamesshould be in a different row.

Related

Insert into with returning into :array

i want to add multiple rows to table in oracle and get back added values to python list
the returned column is the primary key and is generated on the oracle side
the code I wrote doesn't work, which is understandable, but I don't know how to write otherwise. help me please
out_id = cursor.var(cx_Oracle.NUMBER)
batch = []
for i in range(3):
batch.append({'val': i})
ins = 'INSERT INTO table (col1) VALUES (:val) RETURNING table.col2 into :out_id'
cursor.executemany(ins, batch)
print(list_out_id.getvalue())
I get an error
ORA-01036: illegal variable name/number
#e.burenina
A few observations here:
I am assuming col2 is an AUTO INCREMENT Number field
I am assuming you meant to use out_id instead of list_out_id in the last line
arraysize parameter should be included when you define the cursor variable out-id. i.e., out_id = cursor.var(cx_Oracle.NUMBER,arraysize=3).
cx_Oracle.NUMBER will return any number in Python float format(e.g., 0.0, 1.0 etc.). So if the returned data is an integer,
please use out_id = cursor.var(int,arraysize=3)
The batch list should include both the input 'val' and the output variable out_id
batch.append({'val':i,'out_id':out_id})
The following code should return the required values:
import cx_Oracle
# Establish the database connection - Add your DB details here
connection = cx_Oracle.connect(user="hr", password="hr", dsn="localhost/orclpdb")
# Obtain a cursor
cursor = connection.cursor()
# set the input and output variables
out_id = cursor.var(int, arraysize=3)
batch = []
for i in range(3):
batch.append({'val': i, 'out_id': out_id})
# set the SQL statement
ins = 'INSERT INTO table(col1) VALUES (:val) RETURNING table.col2 into :out_id'
# run the query and fetch the output into 'out_id' variable
cursor.executemany(ins, batch)
# print the out_id in a list
print(out_id.values)
Note: Please ensure that you do a connection.commit() at the end, in case you want to commit your code changes to the DB
Also make changes to the arraysize parameter based on the number of rows returned by your INSERT statement.
For printing output in the format
[{'val': 1, 'out_id': id_1}, {'val': 2, 'out_id': id_2}, {...}]
the following code will work
import cx_Oracle
# Establish the database connection - Add your DB details here
connection = cx_Oracle.connect(
user="hr", password="hr", dsn="localhost/orclpdb")
# Obtain a cursor cursor = connection.cursor()
# set the input and output variables
out_id = cursor.var(int, arraysize=3)
batch = []
for i in range(3):
batch.append({'val': i, 'out_id': out_id})
# set the SQL statement
ins = 'INSERT INTO table(col1) VALUES (:val) RETURNING table.col2 into :out_id'
# run the query and fetch the output into 'out_id' variable
cursor.executemany(ins, batch)
#print the output in the required format
for i in range(3):
batch[i]['out_id'] = batch[i]['out_id'].getvalue(i)
print(batch)
print(out_id.values)
# Do a commit to the table if required
# connection.commit()

Get data from select statement mysql.connector

How do I store the content a select statement into a variable?
I have a table called testing with 2 fields: ID, and events.
I do SELECT * FROM testing WHERE id=1; to get the row, but how do I get the data in the events column of that row and store it into a variable x? I couldn't find how to do this online.
I suppose you want to do this in python and I suppose you have already connected your database and put it in a variable named conn
sql = "select * from Laptop"
cursor = conn.cursor()
cursor.execute(sql)
x = cursor.fetchall()
and your result will be stored in the x variable

Value error inserting into Postgres table with psycopg2

I've been trying to use this piece of code:
# df is the dataframe
if len(df) > 0:
df_columns = list(df)
# create (col1,col2,...)
columns = ",".join(df_columns)
# create VALUES('%s', '%s",...) one '%s' per column
values = "VALUES({})".format(",".join(["%s" for _ in df_columns]))
#create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt = "INSERT INTO {} ({}) {}".format(table,columns,values)
cur = conn.cursor()
cur = db_conn.cursor()
psycopg2.extras.execute_batch(cur, insert_stmt, df.values)
conn.commit()
cur.close()
So I could connect into Postgres DB and insert values from a df.
I get these 2 errors for this code:
LINE 1: INSERT INTO mrr.shipments (mainFreight_freight_motherVesselD...
psycopg2.errors.UndefinedColumn: column "mainfreight_freight_mothervesseldepartdatetime" of relation "shipments" does not exist
for some reason, the columns can't get the values properly
What can I do to fix it?
You should not do your own string interpolation; let psycopg2 handle it. From the docs:
Warning Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint.
Since you also have dynamic column names, you should use psycopg2.sql to create the statement and then use the standard method of passing query parameters to psycopg2 instead of using format.

FLASK/sqlite3 select query that returns values of data type decimal or numeric [duplicate]

Is it possible for me to take data stored in a sqlite3 table and use it as a Python variable? I'm looking for something that might be similar to this pseudo-code:
import sqlite3
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
variable = cursor.execute("fetch data from table")
To read a single value from a table, use a SELECT query that returns a result with a single row and a single column:
for row in cursor.execute("SELECT MyColumn FROM MyTable WHERE ID = ?", [123]):
variable = row[0]
break
else:
variable = 0 # not found

Merge tables from two different databases - sqlite3/Python

I have two different SQLite databases XXX and YYY.
XXX contains table A and YYY contains B respectively.
A and B have same structure(columns).
How to append the rows of B in A in Python - SQLite API.
After appending A contains rows of A and rows of B.
You first get a connection to the database using sqlite3.connect, then create a cursor so you can execute sql. Once you have a cursor, you can execute arbitrary sql commands.
Example:
import sqlite3
# Get connections to the databases
db_a = sqlite3.connect('database_a.db')
db_b = sqlite3.connect('database_b.db')
# Get the contents of a table
b_cursor = db_b.cursor()
b_cursor.execute('SELECT * FROM mytable')
output = b_cursor.fetchall() # Returns the results as a list.
# Insert those contents into another table.
a_cursor = db_a.cursor()
for row in output:
a_cursor.execute('INSERT INTO myothertable VALUES (?, ?, ...etc..., ?, ?)', row)
# Cleanup
db_a.commit()
a_cursor.close()
b_cursor.close()
Caveat: I haven't actually tested this, so it might have a few bugs in it, but the basic idea is sound, I think.
This is a generalized function and should be customized to your particular environment. To do this, you may structure the "dynamically determine SQL expression requirements" section with the static SQL parameters (rather than PRAGMA table_info). This should improve performance.
import sqlite3
def merge_tables(cursor_new: sqlite3.Cursor, cursor_old: sqlite3.Cursor, table_name: str, del_old_table: bool = False) -> None:
'''
This function merges the content of a specific table from an old cursor into a new cursor.
:param cursor_new: [sqlite3.Cursor] the primary cursor
:param cursor_old: [sqlite3.Cursor] the secondary cursor
:param table_name: [str] the name of the table
:return: None
'''
# dynamically determine SQL expression requirements
column_names = cursor_new.execute(f"PRAGMA table_info({table_name})").fetchall()
column_names = tuple([x[1] for x in column_names][1:]) # remove the primary keyword
values_placeholders = ', '.join(['?' for x in column_names]) # format appropriately
# SQL select columns from table
data = cursor_old.execute(f"SELECT {', '.join(column_names)} FROM {table_name}").fetchall()
# insert the data into the primary cursor
cursor_new.executemany(f"INSERT INTO {table_name} {column_names} VALUES ({values_placeholders})", data)
if (cursor_new.connection.commit() == None):
# With Ephemeral RAM connections & testing, deleting the table may be ill-advised
if del_old_table:
cursor_old.execute(f"DELETE FROM {table_name}") # cursor_old.execute(f'DROP TABLE {table_name}')
cursor_old.connection.commit()
print(f"Table {table_name} merged from {cursor_old.connection} to {cursor_new.connection}") # Consider logging.info()
return None

Categories