#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
# prepare a cursor object using cursor() method
cursor = x.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
x.close()
Solve Problems by Coding Solutions - A Complete solution for python programming
Showing posts with label DB. Show all posts
Showing posts with label DB. Show all posts
Database connection test to MySQL
#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
x.close()
#Password: root
#Hostname: localhost
# Database Name in MySQL: test
import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
x.close()
A database with integer key using pickle
import anydbm
db = anydbm.open('integer.db', 'c')
import pickle
# A limitation of anydbm is that the keys and values have to be strings.
# If you try to use any other type, you get an error
# The pickle module can help. It translates almost any type of object into a string suitable
# for storage in a database, and then translates strings back into objects.
k = 1
f=pickle.dumps(k)
db[f] = 'Babu'
print 'Value:',db[f]
print 'Key:',f
db.close()
r=pickle.loads(f)
print 'Key Value back:',r
db = anydbm.open('integer.db', 'c')
import pickle
# A limitation of anydbm is that the keys and values have to be strings.
# If you try to use any other type, you get an error
# The pickle module can help. It translates almost any type of object into a string suitable
# for storage in a database, and then translates strings back into objects.
k = 1
f=pickle.dumps(k)
db[f] = 'Babu'
print 'Value:',db[f]
print 'Key:',f
db.close()
r=pickle.loads(f)
print 'Key Value back:',r
Create database and store key value pairs
import anydbm
db = anydbm.open('captions.db', 'c')
db['c1'] = 'Photo of John Cleese.'
print db['c1']
db['c2'] = 'Photo of John Cleese doing a silly walk.'
print db['c2']
for key in db:
print key
db.close()
Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing
anydbm in python.org package
dbm in anaconda package
db = anydbm.open('captions.db', 'c')
db['c1'] = 'Photo of John Cleese.'
print db['c1']
db['c2'] = 'Photo of John Cleese doing a silly walk.'
print db['c2']
for key in db:
print key
db.close()
Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing
anydbm in python.org package
dbm in anaconda package
Subscribe to:
Posts (Atom)