I would like to know if there's a way to create an user with python on a SQL Server 2019? Because I succeed to connect and create a database on the server but I can't connected to the database then...
Here's a bit of my code.
def create_user(self):
global date
utilisateur = self.nom_utilisateur.get()
password = self.password.get()
dbname = utilisateur + str(date.year)
mydb = pyodbc.connect(driver='{SQL Server}', host='DELL-G3\SQLEXPRESS', iud=utilisateur, psw=password)
mydb.autocommit = True
cursor = mydb.cursor()
cursor.execute("CREATE DATABASE %s" % dbname)
try:
mkuser = utilisateur
mkpass = password
creation = "CREATE USER '%s'#'DELL-G3\SQLEXPRESS' IDENTIFIED BY '%s' FROM EXTERNAL PROVIDER" % (mkuser, mkpass)
cursor.execute(creation)
grant = "GRANT SELECT, INSERT ON *.* TO '%s'#'DELL-G3\SQLEXPRESS'" % (mkuser)
cursor.execute(grant)
except sqlite3.OperationalError:
print('error')
mydb.close()
Related
I am facing a weird issue. I have the following code. The INSERTS go well but the update query does not work at all. The rowcount is also shown 1 still when I check in Table Plus it does not reflect.
When I directly run the query UPDATE shop_links set product_status = 3 where shop_url = 'https://example.com' in TablePlus it does show record.
The irony is, the update query which set to 1 works just fine and updates instantly
import mysql.connector
def get_connection(host, user, password, db_name):
connection = None
try:
# connection = pymysql.connect(host=host,
# user=user,
# password=password,
# db=db_name,
# charset='utf8',
# max_allowed_packet=1073741824,
# cursorclass=pymysql.cursors.DictCursor)
connection = mysql.connector.connect(
host=host,
user=user,
use_unicode=True,
password=password,
database=db_name
)
connection.set_charset_collation('utf8')
print('Connected')
except Exception as ex:
print(str(ex))
finally:
return connection
with connection.cursor() as cursor:
sql = 'INSERT INTO {} (shop_url,product_url) VALUES (%s, %s)'.format(TABLE_FETCH_PRODUCTS)
cursor.executemany(sql, records)
connection.commit()
with connection.cursor() as cursor:
# Update the shop URL
# sql = "UPDATE {} set product_status = 3 where shop_url = '{}' ".format(TABLE_FETCH, shop_url)
sql = "UPDATE {} set product_status = 3 where shop_url = %s ".format(TABLE_FETCH, shop_url)
print(sql)
print('----------------------------------------------------------------')
cursor.execute(sql, (shop_url,))
connection.commit()
I am trying to insert values into a table within my redshift cluster, it is connected as I can read the table but I can't insert values on it. When I use SELECT statements it works fine but when I try to insert values from lambda function, it is aborted with no error or log info about why was it aborted.
The query part is like this:
conn = psycopg2.connect(dbname = 'dev',
host =
'redshift-cluster-summaries.c0xcgwtgz65l.us-east-2.redshift.amazonaws.com',
port = '5439',
user = '****',
password = '****%')
cur = conn.cursor()
cur.execute("INSERT INTO public.summaries(topic,summary)
values('data', 'data_summary');")
#print(cur.fetchone())
cur.close()
conn.close()
As I said, there is no log information about why was it aborted, neither it is giving me any kind of error. Actually, when I just use a Select statement, it works.
Is there anyone who can guide me through what could be going on?
You forgot to do conn.commit()
conn = psycopg2.connect(dbname = 'dev',
host = 'redshift-cluster-summaries.c0xcgwtgz65l.us-east-2.redshift.amazonaws.com',
port = '5439',
user = '****',
password = '****%')
cur = conn.cursor()
cur.execute("INSERT INTO public.summaries(topic,summary) values('data', 'data_summary');")
cur.close()
conn.commit()
conn.close()
a bit improved way to run this
from contextlib import contextmanager
#contextmanager
def cursor():
with psycopg2.connect(dbname = 'dev',
host = 'redshift-cluster-summaries.c0xcgwtgz65l.us-east-2.redshift.amazonaws.com',
port = '5439',
user = '****',
password = '****%') as conn:
try:
yield conn.cursor()
finally:
try:
conn.commit()
except psycopg2.InterfaceError:
pass
def run_insert(query):
with cursor() as cur:
cur.execute(query)
cur.close()
run_insert("INSERT INTO public.summaries(topic,summary) values('data', 'data_summary');")
I am trying to run the following code which executes with no issues. The sql that is produced is, "INSERT INTO account_login (groupone_account_id, login_date) VALUES ('100', '10:10:00') which has no syntax errors and executed successfully. But when I check the table, the id has not been inserted. I can complete select queries successfully.
The reason why I created database_connection is because it is an external connection and I wanted to isolate it to be able to test the databse connection easier.
def create_groupone_account_login(groupone_account_id):
groupone_account_login_created = False
cursor = database_connection("cursor")
time = datetime.utcnow().isoformat()[:-3] + 'Z'
sql_create_account_login = "INSERT INTO account_login (groupone_account_id, login_date) VALUES ('%s', '%s')" % (
groupone_account_id, time)
cursor.execute(sql_create_account_login)
connection = database_connection("connection")
connection.commit()
cursor.close()
groupone_account_login_created = True
return groupone_account_login_created
def database_connection(variable):
resp_dict = json.loads(get_secret())
endpoint = resp_dict.get('host')
username = resp_dict.get('username')
password = resp_dict.get('password')
database_name = resp_dict.get('dbname')
port = resp_dict.get('port')
connection = pymysql.connect(host=endpoint, user=username, passwd=password, db=database_name, port=port)
cursor = connection.cursor()
if variable == "connection":
return connection
else:
return cursor
I have a procedure called'my' in mysql. The query is as below
CREATE DEFINER=`as`#`%` PROCEDURE `my`()
BEGIN
delete from my1 where datediff(curdate(), Date_) <= 20;
insert into my1
select
t.idx auto_increment,
t.date_ as 'Date_',
t.brand as 'Br',
concat(t.so_, '_', t.me_) as 'Md'
from
(select idx, date_, brand, so_, me from md where datediff(curdate(), Date_) <= 20) as t
group by t.date_, t.br;
END
To run this procedure in Python, I wrote the following code using pymysql.
user = 'xxxx'
passw = 'xxxxx'
host = 'xxxx'
port = 3306
database = 'DBxxxx'
conn = pymysql.connect(host=host,
port=port,
user=user,
password=passw,
database=database,
charset='utf8',
local_infile=1)
try:
with conn.cursor() as cursor:
cursor.callproc('my')
conn.commit()
conn.close()
except:
conn.close()
But it doesn't work well. Are there any mistakes in the code I wrote? The procedure I run in mysql works fine
I need to execute multiple MySQL select queries in Python. I am thinking of doing it in this way:
connection = mysql.connector.connect(host = HOSTNAME, user = USERNAME, passwd = PASSWORD, db = DATABASE, port=PORT)
cursor = connection.cursor()
try:
query_table0 = 'SELECT %s FROM %s'%(COLUMN.get(TABLES[0]),TABLES[0])
query_table1 = 'SELECT %s FROM %s'%(COLUMN.get(TABLES[1]),TABLES[1])
query_table2 = 'SELECT %s FROM %s'%(COLUMN.get(TABLES[2]),TABLES[2])
cursor.execute(query_table0)
result_table0 = cursor.fetchall()
cursor.execute(query_table1)
result_table1 = cursor.fetchall()
cursor.execute(query_table2)
result_table2 = cursor.fetchall()
finally:
connection.close()
Is there any more optimised way of executing multiple SQL statements in Python?