How to union or make the multiple results sets to one, so that I can fetch the data to python dataframe
EXEC sp_MSforeachdb
#command1 = '
IF not exists(select 1 where ''?'' in (''master'',''model'',''msdb'',''tempdb'',''dpa_dpa_sw1'',''dpa_repository''))
EXEC [?].dbo.sp_MSforeachtable
#command1 = ''SELECT TOP 1 db_name() AS DB_Name, ''''&'''' AS Table_name , * from &'', #replacechar=''&'' '
Python Trial
def db_connect ():
server, username, password = 'xx.xx.xx.xx', 'sa', 'xxxx'
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=' + server
+ ';UID=' + username + ';PWD=' + password)
cursor = conn.cursor()
query = ("EXEC sp_MSforeachdb #command1 = 'IF not exists(select 1 where ''?'' "
"in (''master'',''model'',''msdb'',''tempdb'')) EXEC [?].dbo.sp_MSforeachtable"
" #command1 = ''SELECT TOP 1 db_name() AS DB_Name, ''''&'''' AS Table_name"
" , * from &'', #replacechar=''&'' ';")
df = pd.read_sql(query, conn)
conn.close()
return df
df = db_connect()
Result
| DB_Name | Table_name | id | _NAME | _NUMERICID | _VALUE | _TIMESTAMP | _QUALITY |
|---------|---------------------|----|-----------------|------------|--------|-------------------------|----------|
| aaaa | [dbo].[aaa_exhaust] | 1 | aaaa_vib.00.41 | 0 | 2085 | 2022-08-06 00:30:43.517 | 192 |
In the above case I get only 1st result set, I need results of all result sets in dataframe.
I'm not sure if Pandas can handle multiple result sets like that. You can always use pyodbc's cursor functionality to iterate the multiple result sets, though, and construct a DataFrame like the following...
import pandas as pd
import pyodbc
server, username, password = "127.0.0.1,1433", "sa", "StrongPassw0rd"
connstring = "DRIVER={ODBC Driver 17 for SQL Server};SERVER="+server+";UID="+username+";PWD="+password
conn = pyodbc.connect(connstring)
cursor = conn.cursor().execute("""
select 1 as A;
select 2 as B;
select 3 as C;
""")
buffer = []
while True:
# Get the column names for the current result set
columnNames = [col[0] for col in cursor.description]
# Get the data rows for the current result set
for dataRow in cursor.fetchall():
buffer.append({name: dataRow[index] for index, name in enumerate(columnNames)})
# Iterate to the next result set, otherwise we're "done"
if not cursor.nextset():
break
cursor.close()
conn.close()
# Convert the buffer to a DataFrame and display the result
df = pd.DataFrame(buffer)
print(df.to_string())
Which outputs the following:
A B C
0 1.0 NaN NaN
1 NaN 2.0 NaN
2 NaN NaN 3.0
Related
I use SQLite to merge two dataframes I have parameters in. Some work, others don't.
This works:
s = path_autre_qdv+'\\' + fichier_in
DATA = p.read_csv(s)
con = sql.connect('test_db.sqlite')
con.create_function('POWER', 2, sqlite_power)
c = con.cursor()
COM_DETAIL.to_sql('COM_DETAIL_SQL', con, if_exists='replace', index = False)
DATA.to_sql('DATA_SQL', con, if_exists='replace', index = False)
sq = """
SELECT
c.COM_CODE
,c.COM_NAME
,c.COM_LONG
,c.COM_LAT
,d.*
FROM
COM_DETAIL_SQL c
INNER JOIN
DATA_SQL d
ON POWER((c.COM_LONG - d.longitude)/ ? ,2) + POWER((c.COM_LAT - d.latitude)/ ? ,2) <= 1;
"""
par = (0.5, 0.5,)
RES = read_query(con,sq,par)
This doesn't:
s = path_autre_qdv+'\\' + fichier_in
DATA = p.read_csv(s)
con = sql.connect('test_db.sqlite')
con.create_function('POWER', 2, sqlite_power)
c = con.cursor()
COM_DETAIL.to_sql('COM_DETAIL_SQL', con, if_exists='replace', index = False)
DATA.to_sql('DATA_SQL', con, if_exists='replace', index = False)
sq = """
SELECT
c.COM_CODE
,c.COM_NAME
,c.COM_LONG
,c.COM_LAT
,d.*
FROM
COM_DETAIL_SQL c
INNER JOIN
DATA_SQL d
ON POWER((c.COM_LONG - d.longitude)/ ? ,2) + POWER((c.COM_LAT - d.latitude)/ ? ,2) <= 1
AND d.? IS NOT NULL AND d.? IS NOT NULL AND d.? IS NOT NULL;
"""
par = (0.5, 0.5,cinqv,meanv,nonentecinqv,)
RES = read_query(con,sq,par)
Difference is last filter, with three new parameters: cinqv, meanv and nonentecinqv given in input of the function, and are columns of DATA dataframe.
The error :
OperationalError: near "?": syntax error
from dataa import *
dataman = mysql.connector.connect(host='localhost', user='root', passwd='12345678', database='basictest')
databoy = dataman.cursor()
with open('data.txt') as g:
data = g.read().split('\n')
vl = ['']
vm = ['SELECT Name FROM Patient', 'SELECT DOB FROM Patient', 'SELECT Email FROM Patient',
'SELECT Username FROM Patient', 'SELECT Password FROM Patient', 'SELECT Mobile FROM Patient']
x = 0
for j, i in zip(data, vm):
if
vl[0] = j
x += 1
databax = ("INSERT INTO Patient(Name) VALUES(%s)")
d = [('a')]
databoy.execute(databax, d)
dataman.commit()
print(x)
I want to append each value of J to each column seperately. Only thing I can do is that append all of that to same column . on addition of other column, it throws an error. What can I do. No use of classes will be apppreciated. Can I select each column seperately and update it each time I update the form. This is the registration form of a application
For sql_select_query = 'select * from table', you can use pymysql python module to connect to MySql database and cursor.description to extract values.
def conn():
myDb=pymysql.connect(server,user,password,database)
return myDb
dbc = conn() #database connection
dbCursor = dbc.cursor() # cursor
# gathers all column field names and their type
field_names_and_type = [desc[:2] for desc in dbCursor.description]
Example output:
print(field_names_and_type)
[('ItemId', 1), ('ItemName', 3)]
type 1 is nvchar
type 3 is int
Question: how to i map these?
I've checked pymysql docs but could not find the mapping for cursor.description output.
Pymysql type codes are defined in pymysql.constants.FIELD_TYPE
DECIMAL = 0
TINY = 1
SHORT = 2
LONG = 3
FLOAT = 4
DOUBLE = 5
NULL = 6
TIMESTAMP = 7
LONGLONG = 8
INT24 = 9
DATE = 10
TIME = 11
DATETIME = 12
YEAR = 13
NEWDATE = 14
VARCHAR = 15
BIT = 16
JSON = 245
NEWDECIMAL = 246
ENUM = 247
SET = 248
TINY_BLOB = 249
MEDIUM_BLOB = 250
LONG_BLOB = 251
BLOB = 252
VAR_STRING = 253
STRING = 254
GEOMETRY = 255
CHAR = TINY
INTERVAL = ENUM
I had a query that should return all permittedUserId when userId matched with what was supplied my the user. I wanted to use the result of the query for a different query and noticed I kept getting the same output.
sql = "SELECT permittedUserId FROM PermittedSharedUSer WHERE PermittedSharedUSer.userId = %s"
Here is what the table looks like
Why do I get this output?
[{'num': 3}, {'num': 3}, {'num': 3}, {'num': 3}]
The output I want should be number 7,6,2,3, not 3,3,3,3
Here's the whole code:
self.ensureConnected()
applicationUser = (content['userID'], )
sql = "SELECT permitedUserId FROM PermittedSharedUSer WHERE PermittedSharedUSer.userId = %s"
crows = self.cursor.execute(sql, applicationUser)
result = self.cursor.fetchall()
if(len(result) > 0 ):
d = collections.OrderedDict()
objects_list = []
for row in result:
d['num'] = row[0]
#sqlname = "SELECT name , username FROM Users WHERE Users.id=%s"
#valname = (row[0],)
#self.cursor.execute(sqlname,valname)
#resultName = self.cursor.fetchall()
#for row2 in resultName:
# d['name'] = row2[0]
# d['username'] = row2[1]
objects_list.append(d)
return (json.dumps(dict(data = objects_list), default=str), 200)
elif(crows ==None):
return (json.dumps(dict(data = "empty")), 200)
Here's a sample table which I created in SQL
and the code to fetch the data from it
import mysql.connector
db = mysql.connector.connect(host='localhost',user='root',passwd='5548',database='test')
cursor =db.cursor()
n='1'
cursor.execute("select permission from test where userid = %s",(n,))
v = cursor.fetchall()
for i in v:
print(i[0],end=" ")
and the output is
7 6 2 4
Hence, the way you are establishing the connection and retrieving the data is fine.
Looking at the for loop - I can see the probable error
objects_list.append(d)
should be
objects_list.append(d['num'])
The code should look like this
applicationUser = (content['userID'], )
sql = "SELECT permitedUserId FROM PermittedSharedUSer WHERE PermittedSharedUSer.userId = %s"
crows = self.cursor.execute(sql, applicationUser)
result = self.cursor.fetchall()
if(len(result) > 0 ):
d = collections.OrderedDict()
objects_list = []
for row in result:
d['num'] = row[0]
#sqlname = "SELECT name , username FROM Users WHERE Users.id=%s"
#valname = (row[0],)
#self.cursor.execute(sqlname,valname)
#resultName = self.cursor.fetchall()
#for row2 in resultName:
# d['name'] = row2[0]
# d['username'] = row2[1]
objects_list.append(d['num'])
I'm busy with a script to get some data from the database and arrange it for output later and I would like to know how to clip the variables being returned
I have my script
# CONNECT TO DATABASE
#=====================================================================
varPgSQL = connectToDatabase()
# PREPARE SQL STATEMENTS
#=====================================================================
cur_users = varPgSQL.cursor()
cur_users.execute("prepare cur_users as " +
"select * from users " +
"where usr_idno > $1")
cur_details = varPgSQL.cursor()
cur_details.execute("prepare cur_details as " +
"select * from details " +
"where dtl_usr_idno = $1")
# EXECUTE SQL STATEMENTS
#=====================================================================
la_results = []
cur_users.execute("execute cur_users(%s)", str(0))
for lr_user in cur_users:
cur_details.execute("execute cur_details(%s)", str(lr_user[0]))
for lr_detail in cur_details:
# STORE RESULTS
la_results.append({
"usr_idno": lr_user[0],
"usr_name": lr_user[1],
"dtl_usr_idno": lr_detail[0],
"dtl_usr_accn": lr_detail[1],
"dtl_usr_accs": lr_detail[2]
})
# CLOSE CONNECTION
#=====================================================================
varPgSQL.close()
# CHECK RESULTS
#=====================================================================
for lr_result in la_results:
print(
" | " +
lr_result["usr_name"] +
" | " +
lr_result["dtl_usr_accn"] +
" | " +
lr_result["dtl_usr_accs"] +
" | "
)
The output of this code though is not clipping the variables, the output is
| mavis | service acc | active |
Which is what I expected because it's the length of the fields in the database but is it possible to clip the variables for output to achieve
| mavis | service acc | active |
If the gaps are being created by whitespace you can use the built in String method strip()
If its is a database artifact you may have to give us more information.