How to take table name from user input in flask? - python

#app.route("/CreateAScheme",methods=['GET','POST'])
def CreateAScheme():
if request.method == 'POST':
userDetails = request.form
Scheme = userDetails['Scheme']
con = MySQL.connection.cursor()
con.execute("CREATE TABLE %s (id int NOT NULL AUTO_INCREMENT, Course varchar(255) NOT NULL, Year varchar(255), PRIMARY KEY(id));",(Scheme))
MySQL.connection.commit()
con.close()
flash("Succesfully Created")
return "Succesfully"
return render_template('index.html')
I am Getting an Error "not all arguments converted during string formatting"

Simply wrapping a variable in parentheses, like you're doing in (Scheme) does nothing.
.execute() expects a parameter tuple, no matter if it's just an 1-tuple, i.e. (Scheme,):
con.execute(
"CREATE TABLE %s (id int NOT NULL AUTO_INCREMENT, Course varchar(255) NOT NULL, Year varchar(255), PRIMARY KEY(id));",
(Scheme,)
)

con.execute("CREATE TABLE %s (id int NOT NULL AUTO_INCREMENT, Course varchar(255) NOT NULL, Year varchar(255), PRIMARY KEY(id));" %(Scheme))
"Just Use regular % interpolation"
Thank You #AKX

Related

Python MySQL Query not Working when Run in python but works fine in SQL > CREATE USER

class Setup:
def __init__(self,mydb,User):
self._mydb = mydb
self._User = User
def DataBaseSetup(self):
mycursor = self._mydb.cursor(buffered=True)
mycursor.execute("CREATE DATABASE IF NOT EXISTS StaffDB;")
mycursor.execute("CREATE TABLE IF NOT EXISTS StaffDB.Staff(UserName VarChar(40) PRIMARY KEY,Privlage Int Not Null,FirstName VARCHAR(20),LastName VARCHAR(20),ContactNo VarChar(11),jobTitle VARCHAR(40),startDate DATE,salary INT,DateOfBirth DATE,Address VARCHAR(120))")
mycursor.execute("CREATE TABLE IF NOT EXISTS StaffDB.Classes (RoomNO INT AUTO_INCREMENT PRIMARY KEY, Teacher int, Department VARCHAR(255), ITAccess boolean, ClassCapacity Int)")
mycursor.execute("CREATE TABLE IF NOT EXISTS StaffDB.Pupils (PupilID INT AUTO_INCREMENT PRIMARY KEY, RegClass int, FirstName VARCHAR(20), LastName VARCHAR(20), ContactNo VARCHAR(11), Address VARCHAR(120))")
sql = "INSERT IGNORE INTO StaffDB.Staff (Username,Privlage,FirstName,LastName,ContactNo,jobTitle,startDate,salary,DateOfBirth,Address) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
val = [
("root","0","Fname","LName","07783812342","Admin","2012-12-22","1540000","1997-01-01","Black address")
]
mycursor.executemany(sql, val)
self._mydb.commit()
print(mycursor.rowcount, "record was inserted.")
mycursor.execute("CREATE USER 'Tam'#'localhost' IDENTIFIED BY 'Tam';GRANT ALL ON staffdb.* TO 'Tam'#'localhost';FLUSH PRIVILEGES;",multi=True)
def GetPriv(self):
mycursor = self._mydb.cursor(buffered=True)
mycursor.execute(f"SELECT Privlage FROM StaffDB.Staff WHERE USERNAME LIKE '{self._User}'")
myresult = mycursor.fetchall()
Priv = myresult[0]
return Priv[0]
The Line mycursor.execute("CREATE USER 'Tam'#'localhost' IDENTIFIED BY 'Tam';GRANT ALL ON staffdb.* TO 'Tam'#'localhost';FLUSH PRIVILEGES;",multi=True)
is run shows no errors but has no effect on the DataBase. when i run the Same Code from the database it works perfectly fine.

How can I print schema / table definitions in SQLAlchemy

How can I print out the schema of all tables using sqlalchemy?
This is how I do it using SQLite3: I run an SQL to print out the schema of all tables in the database:
import sqlite3
conn = sqlite3.connect("example.db")
cur = conn.cursor()
rs = cur.execute(
"""
SELECT name, sql FROM sqlite_master
WHERE type='table'
ORDER BY name;
""")
for name, schema, *args in rs:
print(name)
print(schema)
print()
With output that can look like this:
albums
CREATE TABLE "albums"
(
[AlbumId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[Title] NVARCHAR(160) NOT NULL,
[ArtistId] INTEGER NOT NULL,
FOREIGN KEY ([ArtistId]) REFERENCES "artists" ([ArtistId])
ON DELETE NO ACTION ON UPDATE NO ACTION
)
artists
CREATE TABLE "artists"
(
[ArtistId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[Name] NVARCHAR(120)
)
Is there a way to do it with pure sqlalchemy api calls, something better than this?
metadata = sqlalchemy.MetaData()
metadata.reflect(engine)
insp = sqlalchemy.inspect(engine)
for table_name in metadata.tables:
print(table_name)
for column in insp.get_columns(table_name):
for name,value in column.items():
print(' ', end='')
if value:
field = name if value in [True, 'auto'] else value
print(field, end=' ')
print()
Output:
albums
AlbumId INTEGER autoincrement primary_key
Title NVARCHAR(160) autoincrement
ArtistId INTEGER autoincrement
artists
ArtistId INTEGER autoincrement primary_key
Name NVARCHAR(120) nullable autoincrement
This bit in the SQLAlchemy docs may help: they suggest doing this:
def dump(sql, *multiparams, **params):
print(sql.compile(dialect=engine.dialect))
engine = create_engine('postgresql://', strategy='mock', executor=dump)
metadata.create_all(engine, checkfirst=False)

Unable to insert into MySQL database

I am trying to insert something in the MySQL database using the following code, and even though I am not getting any exception or errors, there's nothing in the Table user. In crusor.execute() I tried encapsulating the column names with single quotes and without also using the backtacks nothing worked for me.
import MySQLdb as sql
class DataBaseInteraction:
def __init__(self):
shost = "127.0.0.1"
suser = "root"
spassword = ""
sdb = "gui"
connection = sql.connect(host=shost,
user=suser,
password=spassword,
db=sdb)
try:
self.cursor = connection.cursor()
print("sucksess")
except Exception as e:
print("The Exception was" + str(e))
self.createuser("UserName", "Name", "Email", "Password")
def createuser(self, username, namee, password, email):
print("reached here")
try:
self.cursor.execute("""INSERT INTO user (`UserName`, `Name`, `Email`, `Password`) VALUES ({},{},{},{})""".format(username,
namee,
email,
password))
print("SuckSess")
except Exception as e:
print("Exception was "+str(e))
if __name__ == "__main__":
a = DataBaseInteraction()
Here's the query I used to make the table
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`UserName` varchar(255) NOT NULL,
`Name` varchar(255) NOT NULL,
`Email` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`CreationDate` date DEFAULT NULL,
`LoggedIn` tinyint(1) NOT NULL DEFAULT '0',
`LatestLoginTime` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You are not committing the transaction. After self.cursor.execute statement do self.connection.commit() or set autocommit=True at the time of creating connection. Ex:
connection = sql.connect(host=shost,
user=suser,
password=spassword,
db=sdb,
autocommit=True)

MariaDB duplicates being inserted

I have the following Python code to check if a MariaDB record exists already, and then inserting. However, I am having duplicates being inserted. Is there something wrong with the code, or is there a better way to do it? I'm new to using Python-MariaDB.
import mysql.connector as mariadb
from hashlib import sha1
mariadb_connection = mariadb.connect(user='root', password='', database='tweets_db')
# The values below are retrieved from Twitter API using Tweepy
# For simplicity, I've provided some sample values
id = '1a23bas'
tweet = 'Clear skies'
longitude = -84.361549
latitude = 34.022003
created_at = '2017-09-27'
collected_at = '2017-09-27'
collection_type = 'stream'
lang = 'us-en'
place_name = 'Roswell'
country_code = 'USA'
cronjob_tag = 'None'
user_id = '23abask'
user_name = 'tsoukalos'
user_geoenabled = 0
user_lang = 'us-en'
user_location = 'Roswell'
user_timezone = 'American/Eastern'
user_verified = 1
tweet_hash = sha1(tweet).hexdigest()
cursor = mariadb_connection.cursor(buffered=True)
cursor.execute("SELECT Count(id) FROM tweets WHERE tweet_hash = %s", (tweet_hash,))
if cursor.fetchone()[0] == 0:
cursor.execute("INSERT INTO tweets(id,tweet,tweet_hash,longitude,latitude,created_at,collected_at,collection_type,lang,place_name,country_code,cronjob_tag,user_id,user_name,user_geoenabled,user_lang,user_location,user_timezone,user_verified) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", (id,tweet,tweet_hash,longitude,latitude,created_at,collected_at,collection_type,lang,place_name,country_code,cronjob_tag,user_id,user_name,user_geoenabled,user_lang,user_location,user_timezone,user_verified))
mariadb_connection.commit()
cursor.close()
else:
cursor.close()
return
Below is the code for the table.
CREATE TABLE tweets (
id VARCHAR(255) NOT NULL,
tweet VARCHAR(255) NOT NULL,
tweet_hash VARCHAR(255) DEFAULT NULL,
longitude FLOAT DEFAULT NULL,
latitude FLOAT DEFAULT NULL,
created_at DATETIME DEFAULT NULL,
collected_at DATETIME DEFAULT NULL,
collection_type enum('stream','search') DEFAULT NULL,
lang VARCHAR(10) DEFAULT NULL,
place_name VARCHAR(255) DEFAULT NULL,
country_code VARCHAR(5) DEFAULT NULL,
cronjob_tag VARCHAR(255) DEFAULT NULL,
user_id VARCHAR(255) DEFAULT NULL,
user_name VARCHAR(20) DEFAULT NULL,
user_geoenabled TINYINT(1) DEFAULT NULL,
user_lang VARCHAR(10) DEFAULT NULL,
user_location VARCHAR(255) DEFAULT NULL,
user_timezone VARCHAR(100) DEFAULT NULL,
user_verified TINYINT(1) DEFAULT NULL
);
Add unique constant to tweet_has filed.
alter table tweets modify tweet_hash varchar(255) UNIQUE ;
Every table should have a PRIMARY KEY. Is id supposed to be that? (The CREATE TABLE is not saying so.) A PK is, by definition, UNIQUE, so that would cause an error on inserting a duplicate.
Meanwhile:
Why have a tweet_hash? Simply index tweet.
Don't say 255 when there are specific limits smaller than that.
user_id and user_name should be in another "lookup" table, not both in this table.
Does user_verified belong with the user? Or with each tweet?
If you are expecting millions of tweets, this table needs to be made smaller and indexed -- else you will run into performance problems.

How to return the last primary key after INSERT in pymysql (python3.5)?

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;
for example, how to get the primary key id of the last record that I insert into the table by cursor.execute("insert into ...", ...)?
after inserting,You can get it as:
cursor.execute('select LAST_INSERT_ID()') or use cursor.lastrowid

Categories

Resources