In the middle reaches of the underlying database using python operation method

cursor is a Cursor object, the cursor is a realization of iterator (def__iter __ ()) and generators (yield) of MySQLdb objects, this time cursor also no data, only until fetchone () or fetchall () when it returns a tuple tuple, is supported len () and index () operation, which is the reason it is iterator. But why is this generator it? Because the cursor can only be used once per run out of time after recording its position, until the next time you take that then take place rather than start all over again from the cursor, and after all the data has been fetch, the cursor will no longer use value, and that is no longer able to fetch the data.

Database Support

Use simple text can only be achieved retreat has limited functionality, the need to introduce a database, perform more powerful, simple SQLite database used in this section.

SQLite 和PySQLite

sqlite is very well-known open source embedded database software, which can be embedded into other programs to use and provides an interface to SQL queries, very convenient. It's official site for http://www.sqlite.org.

The pysqlite is a python sqlite to provide the api interface, which allows for all operations have become very simple sqlite

In this latter python2.5 version, SQLite's advantage is that it is a package (PySQLite) has been included in the standard library, so we can directly use.

Getting started Operation

SQLite may be introduced as a module called sqlite3. Then you can create a connection to the database file ---- If the file does not exist it will be created ---- by providing a file name:

>>> import sqlite3
>>> conn= sqlite3.connect('somedatabase.db') # 创建数据库
>>>cu =conn.cursor()   #能获得连接的游标

#创建数据表
>>>cu.execute("""create table catalog (
    id integer primary key,
    pid integer,
    name varchar(10) UNIQUE
   )""")

#插入两条数据
>>>cu.execute("insert into catalog values(0,0,'name1')")
>>>cu.execute("insert into catalog values(1,0,'name2')")
>>>conn.commit()

#选择(select)
>>>cu.execute("select * from catalog")
>>>cu.fetchall() 
[(0, 0, 'name1'), (1, 0, 'name2')]
>>>cu.execute("select * from catalog where id = 1")
>>>cu.fetchall()
[(1, 0, 'name2')]

#修改(update)
>>>cu.execute(“update catalog set name=’name2′ where id = 0″)
>>> cx.commit()
>>> cu.execute(“select * from catalog”)
>>> cu.fetchone()
(0, 0, ‘name2′)

#删除(delete)
>>>cu.execute(“delete from catalog where id= 1″)
>>> cx.commit()
>>> cu.execute(“select * from catalog”)
>>> cu.fetchall()
[(0, 0, 'name2')]

connection

In order to use the underlying database system, it must first be connected to, the need to use with the connect function name, the function of a plurality of parameters, the parameters depend on the specific use to which the database.

The connect function common parameters:

connect function returns the connection object. This object represents the current session and databases. Connection object supports the following methods;

Connection object methods:

commit method is always available, but if the database does not support transactions, it has no effect. If the connection is closed but there are uncommitted transactions, they will implicitly roll back --- but only when the database can hold support rollback.

rollback method may not be available, because not all databases support transactions (a transaction is a series of actions). If available, then you can "undo" all uncommitted transactions.

cursor method brings us to another topic: cursor object. SQL query through a cursor scan line and check the results. Cursor support more connection method, and may be better used in the program.

cursor:

cu = conn.cursor()

Get connected to the cursor, the cursor can be used to execute SQL queries.

conn.commit()

After completing the insertion and make some changes have been made to ensure the submission, so that it can actually save those changes to the file.

Cursor object methods:

Cursor object properties:

cu.fetchone()

or fetchall () returns all data in the result set, the result is a tuple list. Each tuple field element is arranged in order to build the table. Note that the cursor is stateful, it can record the current record has to take the first few results, so, in general you can only traverse the result set. In the above case, if the execution fetchone () will return null. It should be noted that at the time of testing.

conn.close()

When can all be modified after each database commit, rather than just ready to close submission, ready to turn off the data, using the close method.

Guess you like

Origin www.cnblogs.com/djdjdj123/p/11837743.html