Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 13 ■ DATABASE SUPPORT 289 Table 13-5. Cursor Object Methods Name callproc(name[, params]) close() execute(oper[, params]) executemany(oper, pseq) fetchone() fetchmany([size]) fetchall() nextset() setinputsizes(sizes) setoutputsize(size[, col]) Description Call a named database procedure with given name and params (optional). Close the cursor. Cursor is now unusable. Execute an SQL operation, possibly with parameters. Execute an SQL operation for each param set in a sequence. Fetch the next row of a query result set as a sequence, or None. Fetch several rows of a query result set. Default size is arraysize. Fetch all (remaining) rows as a sequence of sequences. Skip to the next available result set (optional). Used to predefine memory areas for parameters. Set a buffer size for fetching big data values. Table 13-6. Cursor Object Attributes Name Description description Sequence of result column descriptions. Read-only. rowcount The number of rows in the result. Read-only. arraysize How many rows to return in fetchmany. Default is 1. Some of these methods will be explained in more detail in the upcoming text, while some (such as setinputsizes and setoutputsizes) will not be discussed. Please consult the PEP for more details. Types In order to interoperate properly with the underlying SQL databases, which may place various requirements on the values inserted into columns of certain types, the DB API defines certain constructors and constants (singletons) used for special types and values. For example, if you want to add a date to a database, it should be constructed with (for example) the Date constructor of the corresponding database connectivity module. That allows the connectivity module to perform any necessary transformations behind the scenes. Each module is required to implement the constructors and special values shown in Table 13-7.
290 CHAPTER 13 ■ DATABASE SUPPORT Table 13-7. DB API Constructors and Special Values Name Date(year, month, day) Time(hour, minute, second) Timestamp(y, mon, d, h, min, s) DateFromTicks(ticks) TimeFromTicks(ticks) TimestampFromTicks(ticks) Binary(string) STRING BINARY NUMBER DATETIME ROWID Description Creates an object holding a date value Creates an object holding a time value Creates an object holding a timestamp value Creates an object holding a date value from ticks since epoch Creates an object holding a time value from ticks Creates an object holding a timestamp value from ticks Creates an object holding a binary string value Describes string-based column types (such as CHAR) Describes binary columns (such as LONG or RAW) Describes numeric columns Describes date/time columns Describes row ID columns Downloading and Installing pysqlite As mentioned previously, there are many SQL database engines available, with corresponding Python modules. Most of these database engines are meant to be run as server programs, and require administrator privileges even to install them. In order to lower the threshold for playing around with the Python DB API, I’ve chosen to use a tiny database engine called SQLite, which doesn’t have to be run as a standalone server, and which can work directly on local files, instead of with some centralized database storage mechanism. You can download pysqlite from the official Web page, http://pysqlite.org. For Linux systems with package manager systems, chances are you can get pysqlite and SQLite directly from the package manager. The Windows binaries for pysqlite actually include the database engine itself (that is, SQLite), so all you have to do is to download the pysqlite installer corresponding to your Python version, run it, and you’re all set. If you’re not using Windows, and your operating system does not have a package manager where you can find pysqlite and SQLite, you have to get the source packages for pysqlite and SQLite and compile them yourself. You can get the sources for SQLite from the SQLite Web page, http://sqlite.org. (Make sure you get one of the source packages where automatic code generation has already been performed.) Compiling SQLite is basically a matter of following the instructions in the included README file. When subsequently compiling pysqlite, you need to make sure that the compilation process can access the SQLite libraries and include files. If you’ve installed SQLite in some standard location, it may well be that the setup script in the SQLite distribution can find it on its own. In that case, you simply need to execute the following commands:
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 333 and 334: 302 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 335 and 336: 304 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 337 and 338: 306 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 339 and 340: 308 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 341 and 342: 310 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 343 and 344: 312 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 345 and 346: 314 CHAPTER 15 ■ PYTHON AND THE W
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 363 and 364: 332 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
CHAPTER 13 ■ DATABASE SUPPORT 289<br />
Table 13-5. Cursor Object Methods<br />
Name<br />
callproc(name[, params])<br />
close()<br />
execute(oper[, params])<br />
executemany(oper, pseq)<br />
fetchone()<br />
fetchmany([size])<br />
fetchall()<br />
nextset()<br />
setinputsizes(sizes)<br />
se<strong>to</strong>utputsize(size[, col])<br />
Description<br />
Call a named database procedure with given name and<br />
params (optional).<br />
Close the cursor. Cursor is now unusable.<br />
Execute an SQL operation, possibly with parameters.<br />
Execute an SQL operation for each param set in a sequence.<br />
Fetch the next row of a query result set as a sequence, or None.<br />
Fetch several rows of a query result set. Default size is arraysize.<br />
Fetch all (remaining) rows as a sequence of sequences.<br />
Skip <strong>to</strong> the next available result set (optional).<br />
Used <strong>to</strong> predefine memory areas for parameters.<br />
Set a buffer size for fetching big data values.<br />
Table 13-6. Cursor Object Attributes<br />
Name<br />
Description<br />
description<br />
Sequence of result column descriptions. Read-only.<br />
rowcount<br />
The number of rows in the result. Read-only.<br />
arraysize How many rows <strong>to</strong> return in fetchmany. Default is 1.<br />
Some of these methods will be explained in more detail in the upcoming text, while some<br />
(such as setinputsizes and se<strong>to</strong>utputsizes) will not be discussed. Please consult the PEP for<br />
more details.<br />
Types<br />
In order <strong>to</strong> interoperate properly with the underlying SQL databases, which may place various<br />
requirements on the values inserted in<strong>to</strong> columns of certain types, the DB API defines certain<br />
construc<strong>to</strong>rs and constants (single<strong>to</strong>ns) used for special types and values. For example, if you<br />
want <strong>to</strong> add a date <strong>to</strong> a database, it should be constructed with (for example) the Date construc<strong>to</strong>r<br />
of the corresponding database connectivity module. That allows the connectivity module <strong>to</strong><br />
perform any necessary transformations behind the scenes. Each module is required <strong>to</strong> implement<br />
the construc<strong>to</strong>rs and special values shown in Table 13-7.