x_boot

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用python来创建数据库的源码

 1 import MySQLdb
 2 
 3 conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='12345')
 4 cursor=conn.cursor()
 5 cursor.execute("""create database if not exists db_pytest""")
 6 conn.select_db('db_pytest')
 7 cursor.execute("create table tb_test(id int, info varchar(100))")
 8 cursor.close()
 9 
10 print 'hello'

首次接触python,从网上download出如下代码,有如下疑问:

1.第3行代码中,MySQLdb.connect()从字面了解,其意义是连接数据库。

  但是MySQLdb下除了connect()方法还有其它什么方法,到哪里能查找到

2.第4行代码中,cursor应该是个游标吧,conn中有cursor方法,该方法是干什么的,有什么意义,有什么同级别的方法,哪里能够查找到

3.第5行代码中,Cursor有execute方法,该方法的语法,参数,有什么同级的方法

  另,该方法的参数中出现了if not的语法,是sql语句吗?

4.该代码段最后,只有cursor的关闭,没有conn的关闭,是不用还是怎的?

 

posted on 2013-08-15 15:53  x_boot  阅读(5335)  评论(0编辑  收藏  举报