Last active
January 19, 2018 19:59
-
-
Save rcgalbo/c870189285391b526c34ff052563417e to your computer and use it in GitHub Desktop.
creating MySQL with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MySQLdb | |
# Open database connection ( If database is not created don't give dbname) | |
db = MySQLdb.connect("localhost","yourusername","yourpassword","yourdbname" ) | |
# prepare a cursor object using cursor() method | |
cursor = db.cursor() | |
# For creating create db | |
# Below line is hide your warning | |
cursor.execute("SET sql_notes = 0; ") | |
# create db here.... | |
cursor.execute("create database IF NOT EXISTS yourdbname") | |
# create table | |
cursor.execute("SET sql_notes = 0; ") | |
cursor.execute("create table IF NOT EXISTS test (email varchar(70),pwd varchar(20));") | |
cursor.execute("SET sql_notes = 1; ") | |
#insert data | |
cursor.execute("insert into test (email,pwd) values('test@gmail.com','test')") | |
# Commit your changes in the database | |
db.commit() | |
# disconnect from server | |
db.close() | |
#OUTPUT | |
# mysql> select * from test; | |
# +-----------------+--------+ | |
# | email | pwd | | |
# +-----------------+--------+ | |
# | test@gmail.com | test | | |
# +-----------------+--------+ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment