import sqlite3
conn = sqlite3.connect('db.db')
c = conn.cursor()
def log_in(patient):
with conn:
c.execute("""INSERT INTO patient_office_syndrome VALUES
(?, ?, ?, ?, ?, ?, ?, ?)""", (patient.Personal_id,patient.first_name, patient.last_name, patient.age, patient.phone, patient.gender, patient.weight, patient.height))
patient_1 = ('1409903748846541','siri','pat','25','06119433332',0,'80','190')
log_in(patient_1)
conn.commit()
conn.close()
Traceback (most recent call last): File
"c:\Users\User\Desktop\db_and_admin_web\main.py", line 19, in
<module> log_in(patient_1) File
"c:\Users\User\Desktop\db_and_admin_web\main.py", line 11, in
log_in (?, ?, ?, ?, ?, ?, ?, ?)""",
(patient.Personal_id,patient.first_name, patient.last_name,
patient.age, patient.phone, patient.gender, patient.weight,
patient.height)) AttributeError: 'tuple' object has no attribute
'Personal_id'
What is the type of patient?
The syntax in your question implies that you want to use a namedtuple.
For example, define patient to be the following:
from collections import namedtuple
Patient = namedtuple('Patient', ('personal_id', 'first_name', 'last_name', ...))
patient = Patient(42, 'John', 'Doe', ...)
then use it this way:
c.execute("""INSERT INTO patient_office_syndrome VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", (patient.personal_id,patient.first_name, patient.last_name, ...)
Alternatively (and perhaps simpler), you can implement patient as a dict.
i am using sqlite3 in python and whenever i insert data it replace it with the data in the DB instead of adding rows
i couldn't have more than one row..
--
def success(self):
global cursor, conn
wallet = random.randint(1000000000, 9999999999)
if self.checkID:
query = [self.fNEntry.get(), self.lNEntry.get(), self.idEntry.get(), self.pwEntry.get(), self.emEntry.get(),
self.pNEntry.get(), wallet, 1000]
cursor = conn.execute('INSERT INTO STUDENTS(FNAME, LNAME, ID, PASSWORD, EMAIL, PHONE, WALLET, BALANCE) \
VALUES(?, ?, ?, ?, ?, ?, ?, ?);', query)
conn.commit()
I created a function to fill a database with API requests values but since I added new columns to the database, that are modified through a web page, I changed INSERT OR REPLACE by INSERT OR UPDATE(Also tried UPSERT) but the function doesn't run anymore.
Since I'm using Django and VSCode, I have no idea how to print things or see an error (this runs in a separate thread so doesn't crash the webpage).
There might be something wrong with the function but I can't see what so I'm hoping someone can help me out.
def fill_resources():
r = pip._vendor.requests.get(BASE_URL+'/api/resources?&maxResults=500',auth=(USER,PASS))
data0 = json.loads(r.text)
conn = sqlite3.connect('/app/docaret.sqlite3')
c = conn.cursor()
for res in data0['data']:
resId = res['id']
r1 = pip._vendor.requests.get(BASE_URL+'/api/resources/'+str(resId)+'/administrative?&maxResults=500',auth=(USER,PASS))
admin = json.loads(r1.text)
r2 = pip._vendor.requests.get(BASE_URL+'/api/resources/'+str(resId)+'/information?&maxResults=500',auth=(USER,PASS))
info = json.loads(r2.text)
r3 = pip._vendor.requests.get(BASE_URL+'/api/resources/'+str(resId)+'/technical-data?&maxResults=500',auth=(USER,PASS))
tech = json.loads(r3.text)
if res['relationships']['mainManager']['data'] == None:
mainManager = 0
else:
mainManager = res['relationships']['mainManager']['data']['id']
if tech['data']['attributes']['diplomas'] != None:
diplomas = tech['data']['attributes']['diplomas']
else:
diplomas = ''
dipText = ''
for dip in diplomas:
newdip = dip + '#µ§'
dipText += newdip
collab = {
"BoondID": resId,
"lastName": encrypt(res['attributes']['lastName']),
"firstName": encrypt(res['attributes']['firstName']),
"dateOfBirth": encrypt(admin['data']['attributes']['dateOfBirth']),
"placeOfBirth": encrypt(admin['data']['attributes']['placeOfBirth']),
"address": encrypt(info['data']['attributes']['address']),
"postcode": encrypt(info['data']['attributes']['postcode']),
"town": encrypt(info['data']['attributes']['town']),
"country": encrypt(info['data']['attributes']['country']),
"email1": encrypt(res['attributes']['email1']),
"email2": encrypt(info['data']['attributes']['email2']),
"phone1": encrypt(res['attributes']['phone1']),
"phone2": encrypt(res['attributes']['phone2']),
"administrativeComments": encrypt(admin['data']['attributes']['administrativeComments']),
"title": encrypt(res['attributes']['title']),
"diplomas": dipText,
"mainManager": mainManager,
"agency": res['relationships']['agency']['data']['id'],
"healthCareNumber": encrypt(admin['data']['attributes']['healthCareNumber']),
"state": info['data']['attributes']['state']
}
values = (collab['BoondID'],collab['lastName'],collab['firstName'],collab['dateOfBirth'],collab['placeOfBirth'],collab['address'],collab['postcode'],collab['town'],collab['country'],collab['email1'],collab['email2'],collab['phone1'],collab['phone2'],collab['administrativeComments'],collab['title'],collab['mainManager'],collab['agency'],collab['healthCareNumber'],collab['diplomas'],collab['state'])
#collabList.append(collab)
c.execute("INSERT OR UPDATE INTO RESOURCES (BoondID,lastName,firstName,dateOfBirth,placeOfBirth,address,postcode,town,country,email1,email2,phone1,phone2,administrativeComments,title,mainManager,agency,healthCareNumber,diplomas,state) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", values)
conn.commit()
conn.close()
I get the following error when running the function independently:
Traceback (most recent call last):
File "<ipython-input-12-87021414c9f9>", line 1, in <module>
fill_resources()
File "C:/Users/valen/OneDrive/Bureau/DOCARET/tous les tests/testcontracts.py", line 63, in fill_resources
c.execute("INSERT OR UPDATE INTO RESOURCES (BoondID,lastName,firstName,dateOfBirth,placeOfBirth,address,postcode,town,country,email1,email2,phone1,phone2,administrativeComments,title,mainManager,agency,healthCareNumber,diplomas,state) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", values)
OperationalError: near "UPDATE": syntax error
SQLite does not support INSERT OR UPDATE INTO....
You can use UPSERT if your version of SQLite is 3.24.0+.
Assuming that BoondID is the PRIMARY KEY of the table or there is a unique constraint defined for it:
sql = """
INSERT INTO RESOURCES (
BoondID, lastName, firstName, dateOfBirth, placeOfBirth, address,
postcode, town, country, email1, email2, phone1, phone2,
administrativeComments, title, mainManager, agency, healthCareNumber,
diplomas, state
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(BoondID) DO UPDATE SET
(lastName, firstName, dateOfBirth, placeOfBirth, address, postcode, town,
country, email1, email2, phone1, phone2, administrativeComments, title,
mainManager, agency, healthCareNumber, diplomas, state) =
(EXCLUDED.lastName, EXCLUDED.firstName, EXCLUDED.dateOfBirth,
EXCLUDED.placeOfBirth, EXCLUDED.address, EXCLUDED.postcode,
EXCLUDED.town, EXCLUDED.country, EXCLUDED.email1, EXCLUDED.email2,
EXCLUDED.phone1, EXCLUDED.phone2, EXCLUDED.administrativeComments,
EXCLUDED.title, EXCLUDED.mainManager, EXCLUDED.agency,
EXCLUDED.healthCareNumber, EXCLUDED.diplomas, EXCLUDED.state);
"""
c.execute(sql, values)
I was trying to insert the CSV file to an already existing table in the SSMS database table. I have a data column in my data. But I keep getting this error when I try to insert data. Please tell me where am I doing it wrong because server connection and extracting data from the database are fine. Below is the code.
with open("combine.csv", encoding="utf8") as f:
csvreader = csv.reader(f)
csvdata = []
for row in csvreader:
csvdata.append(row)
print(csvdata)
for row in csvdata:
# Insert a row of data
print(row)
if len(row)>=8:
data = [row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7]]
cursor.execute("INSERT INTO BILLING_COPY (DATE, DEPARTMENT_NUMBER, DEPARTMENT_NAME, DIVISION_CODE, DIVISION_NAME, O_T_AMT, R_AMT, U_AMT ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", data)
Error:
File "", line 7, in
cursor.execute("INSERT INTO BILLING_COPY (DATE, DEPARTMENT_NUMBER, DEPARTMENT_NAME, DIVISION_CODE, DIVISION_NAME, O_T_AMT, R_AMT, U_AMT ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", data)
DataError: ('22007', '[22007] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string. (241) (SQLExecDirectW)')
File "", line 7, in
cursor.execute("INSERT INTO BILLING_COPY (DATE, DEPARTMENT_NUMBER, DEPARTMENT_NAME, DIVISION_CODE, DIVISION_NAME, O_T_AMT, R_AMT, U_AMT ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", data)
I think the data type you mentioned in VALUES(?,?,? etc) is not right valid data type, try using it as %d or %s
Here is some example:
mySql_insert_query = """INSERT INTO Laptop (Id, Name, Price, Purchase_date)
VALUES
(10, 'ProductValues SP99', 6459, '2019-12-27') """
cursor = connection.cursor()
cursor.execute(mySql_insert_query)
connection.commit()
My two cents: Better to assign insert query to a variable just like data variable.
Newbie here, trying to import from multiple csv to sql server, the code did run but no data inserted into sql database.
Attached is my code. Maybe the error is lie on the loop.
Please help.
import csv
import pyodbc as p
import os
# Database Connection Info
server = "cld-077\eform"
database = "E-form"
username = "wsmeform"
password = "M1loA1s!"
connStr = (
'DRIVER={ODBC Driver 13 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
# Open connection to SQL Server Table
conn = p.connect(connStr)
# Get cursor
cursor = conn.cursor()
# Assign path to Excel files
print("Inserting!")
folder_to_import = 'C:/Users/ck.law/Desktop/VBFU_NOV/'
print("path")
l_files_to_import = os.listdir(folder_to_import)
print("inside loop")
for file_to_import in l_files_to_import:
if file_to_import.endswith('.csv'):
csv_files = os.path.join(folder_to_import, file_to_import)
csv_data = csv.reader(csv_files)
for row in csv_data:
if len(row) >= 19:
cursor.execute(
"INSERT INTO VesselBFUData(ShortCode,DocDT,PostDT,DocNo,LineItm,GlCode,ExpType,InvRef,VBaseCurrcy,VBaseAmt,DocCurrcy,DocAmt,VendorCode,Description,InvFilePath,InvCreateDT,InvAppvDT,InvArriDT,PoRef)" " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
row)
print("Loop!")
cursor.close()
conn.commit()
conn.close()
print("Script has successfully run!")