用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的關閉,是不用還是怎的?