官术网_书友最值得收藏!

Databases

A relational database is one of the most common ways that enterprises can store data. So, loading from and interacting with databases is essential for most fieldwork. The Python library that we will use is sqlite3 and is included in Anaconda's package. Let's begin by connecting to the database, which is stored in a .db file, and included with the book materials. After we connect to the database, we will create a cursor object that we will use to traverse the object during a query. Next, we will select the entire contents of the boston table with the * condition and limit the rows to five (only so that we can display the output without overloading our console). Finally, we will execute() the query and fetchall(). The data returned from the query is controlled by the search teams (select and limit, in this case). The next section will introduce more common terms used for queries:

import sqlite3
sqlite_file = './data/boston.db'

# connecting to the database file
conn = sqlite3.connect(sqlite_file)

# initialize a cursor obect
cur = conn.cursor()

# define a traversing search
cur.execute("select * from boston limit 5;")

# fetch and print
data = cur.fetchall()
print(data)

The output from the print() statement is 5 records, each with 15 entries, which correspond to the rows and columns of the data table, respectively:

For our example, we use the included database ( .db) file in the place of an actual remote database. This means that we will connect to this file. In practice, you will connect to a remote location by using a network address and login credentials. 

主站蜘蛛池模板: 隆德县| 怀来县| 积石山| 疏勒县| 高碑店市| 西平县| 达日县| 和静县| 鲜城| 垣曲县| 栖霞市| 龙门县| 江安县| 大悟县| 远安县| 宝丰县| 遂宁市| 盐亭县| 蕉岭县| 建德市| 灵璧县| 苏州市| 本溪市| 大化| 石嘴山市| 广宁县| 额济纳旗| 中卫市| 松阳县| 大关县| 莱芜市| 会同县| 墨脱县| 云梦县| 且末县| 青浦区| 梓潼县| 客服| 沁阳市| 高陵县| 闸北区|