cursor is a Cursor object, this cursor is a MySQLdb object that implements iterator (def__iter__()) and generator (yield), there is no data in the cursor at this time, only until fetchone() or fetchall() returns a tuple It is only when fetchone() or fetchall() that a tuple is returned, and only then does it support the len() and index() operations, which is why it is an iterator. But at the same time, why is it a generator? Because the cursor can only be used once, that is, after each use after the record of its location, until the next time to fetch from the cursor when the cursor is fetched again instead of from the beginning, and fetch all the data, the cursor will no longer have the value of the use of the data, that is, no longer be able to fetch to the data.
Database support
The use of simple plain text can only be realized with the function of the retreat limit, the need to introduce the database, to complete the more powerful functions, this section uses the simple database SQLite.
SQLite and PySQLite
sqlite is very famous open source embedded database software, it can be embedded into other programs to use, and provide SQL interface to query, very convenient. Its official website is。
pysqlite, on the other hand, is an api interface to sqlite for python that makes everything sqlite does incredibly easy.
The advantage of SQLite after python 2.5 is that a wrapper for it (PySQLite) is included in the standard library, so we can use it directly.
introductory operation
SQLite can be imported as a module named sqlite3. After that a connection to the database file can be created ---- If the file does not exist it will be created ---- by providing a filename:
>>> import sqlite3 >>> conn= ('') # Create databases >>>cu =() # Can get the cursor of the connection #Creating a data table >>>("""create table catalog ( id integer primary key, pid integer, name varchar(10) UNIQUE )""") # Insert two pieces of data >>>("insert into catalog values(0,0,'name1')") >>>("insert into catalog values(1,0,'name2')") >>>() #Select >>>("select * from catalog") >>>() [(0, 0, 'name1'), (1, 0, 'name2')] >>>("select * from catalog where id = 1") >>>() [(1, 0, 'name2')] #Update >>>(“update catalog set name='name2′ where id = 0″) >>> () >>> (“select * from catalog”) >>> () (0, 0, ‘name2′) #delete >>>(“delete from catalog where id= 1″) >>> () >>> (“select * from catalog”) >>> () [(0, 0, 'name2')]
grout
In order to use the underlying database system, you must first connect to it, and this time you need to use the connect function with a name that has several parameters, and which parameter to use depends on the database.
Common arguments to the connect function:
The connect function returns the connection object. This object represents the current session with the database. The methods supported by the connect object are as follows;
Connection object method:
The commit method is always available, but it does nothing if the database does not support transactions. If the connection is closed but there are uncommitted transactions, they are implicitly rolled back - but only if the database supports holding rollbacks.
The rollback method may not be available because not all databases support transactions (a transaction is a sequence of actions). If it is available, then all uncommitted transactions can be "undone".
The cursor method introduces us to another topic: the cursor object. Sweeping a SQL query through a cursor and examining the results. Cursor joins support more methods and may work better in your program.
Cursor:
cu = ()
You can get a connected cursor that can be used to execute SQL queries.
()
Make sure you have committed after you have inserted and made some changes so that they are actually saved to the file.
Cursor object methods:
Cursor object characteristics:
()
fetchall() returns all the data in the result set, resulting in a list of tuples. Each tuple element is in the order of the fields in which the table was built. Note that the cursor is stateful in that it keeps track of how many records in the result have been fetched, so you can generally only traverse the result set once. In the above case, if you execute fetchone() it will return null. This is something to keep in mind when testing.
()
Instead of committing every time you modify the database, you can commit only when you are ready to close, and use the close method when you are ready to close the data.
summarize
The above is a small introduction to the use of the cursor in python operation database, I hope to help you, if you have any questions please leave me a message, I will reply to you in time. Here also thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!