· 9 years ago · Jul 17, 2017, 11:20 PM
1#!/usr/bin/python
2import MySQLdb
3
4# Connect
5db = MySQLdb.connect(host="localhost",
6 user="appuser",
7 passwd="",
8 db="onco")
9
10cursor = db.cursor()
11
12# Execute SQL select statement
13cursor.execute("SELECT * FROM location")
14
15# Commit your changes if writing
16# In this case, we are only reading data
17# db.commit()
18
19# Get the number of rows in the resultset
20numrows = cursor.rowcount
21
22# Get and display one row at a time
23for x in range(0, numrows):
24 row = cursor.fetchone()
25 print row[0], "-->", row[1]
26
27# Close the connection
28db.close()
29
30import mysql.connector
31cnx = mysql.connector.connect(user='scott', password='tiger',
32 host='127.0.0.1',
33 database='employees')
34
35try:
36 cursor = cnx.cursor()
37 cursor.execute("""
38 select 3 from your_table
39 """)
40 result = cursor.fetchall()
41 print result
42finally:
43 cnx.close()
44
45import pymysql.cursors
46import pymysql
47
48# Connect to the database
49connection = pymysql.connect(host='localhost',
50 user='user',
51 password='passwd',
52 db='db',
53 charset='utf8mb4',
54 cursorclass=pymysql.cursors.DictCursor)
55
56try:
57 with connection.cursor() as cursor:
58 # Create a new record
59 sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
60 cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
61
62 # connection is not autocommit by default. So you must commit to save
63 # your changes.
64 connection.commit()
65
66 with connection.cursor() as cursor:
67 # Read a single record
68 sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
69 cursor.execute(sql, ('webmaster@python.org',))
70 result = cursor.fetchone()
71 print(result)
72finally:
73 connection.close()
74
75# server_version.py - retrieve and display database server version
76
77import MySQLdb
78
79conn = MySQLdb.connect (host = "localhost",
80 user = "testuser",
81 passwd = "testpass",
82 db = "test")
83cursor = conn.cursor ()
84cursor.execute ("SELECT VERSION()")
85row = cursor.fetchone ()
86print "server version:", row[0]
87cursor.close ()
88conn.close ()
89
90import oursql
91
92db_connection = oursql.connect(host='127.0.0.1',user='foo',passwd='foobar',db='db_name')
93cur=db_connection.cursor()
94cur.execute("SELECT * FROM `tbl_name`")
95for row in cur.fetchall():
96 print row[0]
97
98import pymysql.cursors
99connection = pymysql.connect(host='localhost',
100 user='mahdi',
101 password='mahdi',
102 charset='utf8mb4',
103 cursorclass=pymysql.cursors.DictCursor)
104cursor = connection.cursor()
105cursor.execute("CREATE DATABASE IF NOT EXISTS "+database)
106connection.select_db(database)
107sql_create = "CREATE TABLE IF NOT EXISTS "+tablename+(timestamp DATETIME NOT NULL PRIMARY KEY)"
108cursor.execute(sql_create)
109connection.commit()
110cursor.close()
111
112sudo yum install MySQL-python
113sudo apt-get install MySQL-python
114
115from storm.locals import *
116
117# User will be the mapped object; you have to create the table before mapping it
118class User(object):
119 __storm_table__ = "user" # table name
120 ID = Int(primary=True) #field ID
121 name= Unicode() # field name
122
123database = create_database("mysql://root:password@localhost:3306/databaseName")
124store = Store(database)
125
126user = User()
127user.name = u"Mark"
128
129print str(user.ID) # None
130
131store.add(user)
132store.flush() # ID is AUTO_INCREMENT
133
134print str(user.ID) # 1 (ID)
135
136store.commit() # commit all changes to the database
137
138michael = store.find(User, User.name == u"Michael").one()
139print str(user.ID) # 10
140
141print store.get(User, 1).name #Mark