Table is not creating - python

I am making little project with sqlite3, but table is not creating.
import sqlite3
conn = sqlite3.connect("test.sqlite")
c = conn.cursor()
c.execute('''CREATE TABLE students
(rollno real, name text, class real)''')
conn.commit()
#close the connection
conn.close()
see photo

This is not a code error.
Please navigate to where you have the python file running/saved in the DB browser. File -> Open Database and select the test.sqlite file. You will then see something like this:
Code used:
import sqlite3
conn = sqlite3.connect("test.sqlite")
c = conn.cursor()
c.execute('''CREATE TABLE students
(rollno real, name text, class real)''')
conn.commit()
#close the connection
conn.close()

Related

psycopg2 with Postgres fetchall works but not execute

I am trying to execute some Postgres queries with psycopg2.
import psycopg2
conn = psycopg2.connect(
host="test.postgres.database.azure.com",
database="postgres",
user="test",
password="test")
cur = conn.cursor()
sql = "select site_panel from test.ws_sites"
cur.execute(sql)
rows = cur.fetchall()
The query above works well and returns the data but the following query does not delete the table as intended.
cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
Anything wrong I'm doing here?
A query that modifies table data or schema structure needs to be committed:
cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
conn.commit()
Alternatively, place
conn.autocommit = True
after creating the connection.

How to update a table in SQL Server during runtime using bind variable with a placeholder like '?' in python using pyodbc module?

I am writing a function which would save the records updated in the GUI which is made using Tkinter
def save():
conn = pyodbc.connect('Driver={SQL Server};'
'Server=XXXXXXX;'
'Trusted_Connection=yes;')
After connecting to the server,`I am getting values from another function, and updating into my database here,but I am getting an error.
cursor = conn.cursor()
record_id = select_box.get()
cursor.execute("UPDATE homeaddresses SET (?,?,?,?,?,?) where id=id)",
f_name_editor.get(),
l_name_editor.get(),
address_editor.get(),
city_editor.get(),
state_editor.get(),
pincode_editor.get())
conn.commit()
conn.close()
Should be something like
cursor.execute("UPDATE homeaddresses SET f_name=?,l_name,address=?,city=?,state=?,pincode=? where id=?",
f_name_editor.get(),
l_name_editor.get(),
address_editor.get(),
city_editor.get(),
state_editor.get(),
pincode_editor.get(),
record_id )

Why am i getting weakly-referenced-object-no-longer-exists-using-python-and-mysql

i am trying to run this code but am getting weak refrence error at line 6
import mysql.connector as mysql
con=mysql.connect(host='localhost',user='root')
cursor=con.cursor()
cursor.execute('Create database if not exists shivam')
con=mysql.connect(host='localhost',user='root',database='shivam')
cursor.execute('use shivam')
cursor.execute('slect * from item') #this is a table in my database
data=cursor.fetchall()
print(data)
con.commit()
con.close()

cx_oracle executes without error, but does not delete data

I would to remove all customers who's names begin with SCH from my database. When I execute the code below it runs without error, but does not remove the data from the database.
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
after delete you need commit
conn = cx_Oracle.connect(...)
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
conn.commit()
cur.close()
conn.close()

SQLITE in Python not creating tables but creates database

I am trying to connect to SQLITE database and it would seem that the code does create the database, but however it does not create the tables nor does it insert anything in them (most probably because they were never created)
I have SQLite DB Browser and in it I open the database but lack any tables or anything at all. Also I see my database in the folder next to my project.
I have the following code:
import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()
def create_tabe():
c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')
def data_entry():
c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
conn.commit()
c.close()
conn.close()

Categories