-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_mysql.py
More file actions
54 lines (39 loc) · 1.3 KB
/
Copy pathtest_mysql.py
File metadata and controls
54 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import mysql.connector
cnx = mysql.connector.connect(user='python', password='spartak',host='127.0.0.1',database='test')
cursor = cnx.cursor()
cursor.execute("INSERT INTO users (email, password) values (%s, %s)",("ekibalko@franklinamerican.com", "secret"))
cnx.commit()
cursor.close()
cnx.close()
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","python","spartak","test" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT * from users")
# Fetch a single row using fetchone() method.
data = cursor.fetchall()
for row in data:
print(row)
# disconnect from server
db.close()
import peewee as pw
import random
myDB = pw.MySQLDatabase("test", host="localhost", port=3306, user="python", passwd="spartak")
class MySQLModel(pw.Model):
"""A base model that will use our MySQL database"""
class Meta:
database = myDB
class User(MySQLModel):
id = pw.IntegerField(primary_key = True)
email = pw.CharField()
password = pw.CharField()
# etc, etc
# when you're ready to start querying, remember to connect
myDB.connect()
randid = random.randint(1,100000)
user = User(id = randid, email='evgkib@hotmail.com', password='secret')
user.save(force_insert=True)
print user.id
myDB.close()