- Python Data Mining Quick Start Guide
- Nathan Greeneltch
- 270字
- 2021-06-24 15:19:50
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:

- Getting Started with Clickteam Fusion
- 手把手教你玩轉(zhuǎn)RPA:基于UiPath和Blue Prism
- Dreamweaver CS3網(wǎng)頁設(shè)計50例
- 微型計算機控制技術(shù)
- 城市道路交通主動控制技術(shù)
- Embedded Programming with Modern C++ Cookbook
- 電腦上網(wǎng)輕松入門
- Linux Shell編程從初學到精通
- 大數(shù)據(jù)導論
- Drupal高手建站技術(shù)手冊
- Linux Shell Scripting Cookbook(Third Edition)
- Cortex-M3嵌入式處理器原理與應(yīng)用
- Cloudera Hadoop大數(shù)據(jù)平臺實戰(zhàn)指南
- Windows 7來了
- 菜鳥起飛五筆打字高手