在sage中使用mysql数据库

sage默认自带了python环境即使你的系统中没有python sage也是可以正常运行,但是sage在使用一些包的时候优先使用自己的包,比如我系统安装了MySQLdb然后我认为sage已经可以使用了结果出现了如下的错误

import MySQLdb 
       
Traceback (click to the left of this block for traceback)
...
ImportError: No module named MySQLdb
 

当我执行以下操作将mysql包的路径添加到sage的python搜索路径中之后,再导入mysql包是okay的

import sys sys.path.append('/usr/lib64/python2.6/site-packages/') 
         
import MySQLdb 
         
conn=MySQLdb.connect(host="127.0.0.1",user="root",passwd="12345",db="dt",charset="utf8") 
         
cursor = conn.cursor() 
         
cursor.execute("show tables;") 
       
1L
 
data = cursor.fetchone() 
         
data 
         
cursor.execute("select * from test") 
       
4L
 
for i in range(4): print cursor.fetchone() 
       
(1L, None, None, None, None)
(2L, None, 1428418800L, 1433084400L, 1423494000L)
(3L, None, None, None, 1433084400L)
(4L, None, None, None, None)
 
 
         

猜你喜欢

转载自robbiefeng.iteye.com/blog/2217031