The premise is that MySQL is installed, Setuptools is installed, and then refer to Web http://jingyan.baidu.com/article/fedf07377ded3e35ad897750.html to associate MySQL with Django. This makes it possible to associate the model with the database in Django.
If you don't use a Django module, here's a simple introduction to Python's MySQLdb module. The reference http://www.cnblogs.com/rollenholt/archive/2012/05/29/2524327.html is based on a small number of deletions and expansions. The Code section has not been continued. Later use the new need to add again.
import MySQLdb try : Conn =mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 123456 ', db= ' books ', port=3306) #连接数据库 cur =
cur.exec Ute ( "select * from Books_book ' ) #执行数据库语句 cur.close () #关闭游标 conn.close () #关闭数据库 except Mysqldb.error, E: print " mysql Error%d:%s % (E.args[0], e.args[1])
Import MySQLdb
>>> conn=mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 123456 ', port=3306)
>>> cur=conn.cursor () #在进入数据库就取得游标. Similar to the cursor. An excitement might be said as a cursor. Move a cursor to a different location by executing a database statement, such as moving to a library and moving to a table in a library.
>>> Cur.execute ("Use Books") # # #This can also be used conn.select_db ( 'books') # is the same as the database-side use, and then the cur= Conn.cursor () # # #或者conn=mysqldb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 123456 ', db= ' books ', port =3306) #连接数据库books
0L
>>> Cur.execute ("SELECT * from Books_author") #这样就把游标cur调到表books_author. Execute database statements without semicolons, or casing #输出显示一共有三条信息
3L
>>> Cur.fetchone () #每运行一次完毕, outputs the current cursor information, while the cursor moves down to the next index. To see the specific information results, use the Fetchone () and Fetchall () attributes.
(1L, ' Wendy ', ' Xu ', ' [email protected] ')
>>> Cur.fetchone ()#每运行一次, the cursor moves down one index.
(2L, ' Beryl ', ' Li ', ' [email protected]na.com ')
>>> Cur.fetchone ()#每运行一次, the cursor moves down one index. Because altogether three messages, here is the last one.
(3L, ' billly ', ' Xu ', ' [email protected] ')
>>> Cur.fetchone () #因为已经没有后续记录了, so run fetchone here to return empty.
>>> Cur.fetchone ()
>>> cur.scroll (0, ' absolute ') #重新确认游标位置. The first record index in the database is 0. The default is Relative,cur.scroll (2, ' relative ') at the current cursor position, and move backward by 2 bits. Now that the index is the first one, thenrelativeAfter this statement is executed, the cursor stops on the third line. Absolute is an absolute index. Specifies the absolute position of the cursor stop.
>>> A=cur.fetchone ()
>>> a #a是一元元组, the use of tuples requires a serial number index. #a [0]= ' 1L ', a[1]= ' Wendy '
(1L, ' Wendy ', ' Xu ', ' [email protected] ')
>>> A=cur.fetchall ()
>>> a#a是三行元组 #暂时这样理解, not sure.
( 1L, ' Wendy ', ' Xu ', ' [email protected] '), (2L, ' Beryl ', ' Li ', ' [email protected] '), (3L, ' billly ', ' Xu ', ' [email prot Ected]))
>>> A[0]#a [1]=(2L, ' Beryl ', ' Li ', ' [email protected] ')
(1L, ' Wendy ', ' Xu ', ' [email protected] ')
>>> a[0][1] #a [0][0]= ' 1L '
' Wendy '
>>>
obtain known information about the database in this way. It is a tuple that is removed. To remove a column can be as follows
>>> Cur.execute ("SELECT * from Books_author")
3L
>>> V=[i[1] for I in Cur.fetchall ()] #注意是中括号
>>> V
[' Wendy ', ' Beryl ', ' billly ']
>>> cur.description
(' ID ', 3, 1, one, one, 0, 0), (' First_Name ', 253, 6,, 0, 0), (' Last_Name ', 253, 2,,, 0, 0), (' email ', 253, 17, 75, 75, 0, 0))
>>> Cur.execute ("DESCRIBE books_author;") #这个蓝色的部分是值, where Cur.execute go, do what. So the cursor position needs to be adjusted before use. Cur.execute knows the point database language is good.
>>> cur.description
(' Field ', 253, 0, 0), (' Type ', 252, one, 196605, 196605, 0, 0), (' Null ', 253, 2, 3, 3, 0, 0), (' Key ', 253, 3, 3, 3, 0, 0), (' Default ', 252, 0, 196605, 196605, 0, 1), (' Extra ', 253, 14, 30, 30, 0, 0)
>>> cur.scroll (0, ' absolute ')
>>> Cur.fetchmany (2)
(1L, ' Wendy ', ' Xu ', ' [email protected] '), (2L, ' Beryl ', ' Li ', ' [email protected] ')
The MySQLdb module in Python