Skip to content

Commit 95324cc

Browse files
committed
python mysql database operations
1 parent 2dd7903 commit 95324cc

13 files changed

Lines changed: 390 additions & 73 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__author__ = 'Avinash'

python_database_operations/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python_database_operations/.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python_database_operations/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python_database_operations/.idea/python_database_operations.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python_database_operations/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python_database_operations/.idea/workspace.xml

Lines changed: 226 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
__author__ = 'Avinash'
2+
3+
import pymysql
4+
5+
# Open database connection
6+
db = pymysql.connect("localhost", "root", "", "python_tutorials")
7+
8+
# prepare a cursor object using cursor() method
9+
cursor = db.cursor()
10+
11+
# Drop table if it already exist using execute() method.
12+
cursor.execute("DROP TABLE IF EXISTS PERSON")
13+
14+
# Create table as per requirement
15+
sql = """CREATE TABLE PERSON (
16+
ID INT NOT NULL,
17+
FIRST_NAME CHAR(20) NOT NULL,
18+
LAST_NAME CHAR(20),
19+
AGE INT,
20+
SEX CHAR(1),
21+
PRIMARY KEY (ID) )"""
22+
23+
cursor.execute(sql)
24+
25+
# disconnect from server
26+
db.close()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
__author__ = 'Avinash'
2+
import pymysql
3+
4+
# Open database connection
5+
db = pymysql.connect("localhost", "root", "", "python_tutorials")
6+
7+
# prepare a cursor object using cursor() method
8+
cursor = db.cursor()
9+
10+
# Prepare SQL query to DELETE required records
11+
sql = "DELETE FROM PERSON WHERE AGE > '%d'" % (24)
12+
try:
13+
# Execute the SQL command
14+
cursor.execute(sql)
15+
# Commit your changes in the database
16+
db.commit()
17+
except:
18+
# Rollback in case there is any error
19+
db.rollback()
20+
21+
# disconnect from server
22+
db.close()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
__author__ = 'Avinash'
4+
5+
import pymysql
6+
7+
# Open database connection
8+
db = pymysql.connect("localhost", "root", "", "python_tutorials")
9+
10+
# prepare a cursor object using cursor() method
11+
cursor = db.cursor()
12+
13+
# Prepare SQL query to INSERT a record into the database.
14+
sql = """INSERT INTO PERSON(ID, FIRST_NAME,
15+
LAST_NAME, AGE, SEX )
16+
VALUES (1, 'ABC', 'ABCD', 20, 'M')"""
17+
try:
18+
# Execute the SQL command
19+
cursor.execute(sql)
20+
# Commit your changes in the database
21+
db.commit()
22+
except pymysql.Error:
23+
# Rollback in case there is any error
24+
db.rollback()
25+
26+
# disconnect from server
27+
db.close()

0 commit comments

Comments
 (0)