Local Python and SQLite for an after-close China A-share review
Local review series, part 1. The bars stay on your PC, the stack costs nothing, and the later notes build on this file.
This is part 1 of the Chinese series on a local review stack. You can follow it with no prior setup. The next three notes assume this environment, this directory, and this empty database.
Short answer: install Python 3.12 or 3.13 with PATH set, make a virtual environment at D:/quant/.venv, and create daily in D:/quant/astock.db with a primary key on code and date. Stay local if you only review after the close. Use a cloud server if the machine must stay on.
How to choose: Stay on the computer you already use for review when the job only has to run after the close. The stack costs nothing, the bars never leave the machine, and they sit next to the charting app you already open. The cost is that the PC has to be awake. A sleeping laptop will not run the later tasks. If the machine must stay up overnight, start with the cloud server note instead. For a daily review after the close, local is enough. On Windows, install Python 3.12 or 3.13 from python.org and tick Add python.exe to PATH on the first screen. Skip the Microsoft Store build. Its virtual environments misbehave. On macOS, Homebrew is enough: brew install python. Confirm with python --version. If pip is slow from mainland China, point it at the Tsinghua index the Chinese page uses. Then create D:/quant/.venv so this stack does not share packages with anything else, activate it, and install akshare and pandas into it. SQLite needs no separate server. Python's sqlite3 module opens one file, D:/quant/astock.db. DB Browser for SQLite is the free viewer when you want to see the rows. The first table is daily: code, date, open, high, low, close, volume, with a primary key on code and date, so the same bar cannot be stored twice. Prove the path by inserting 600519 on 2025-08-29 and printing that row. This page does not download a watchlist, does not schedule a task, and does not call a model. Those are later notes. It is not the tool-order guide and not the site FAQ.
Who it is for: Someone setting up a China A-share review on their own PC, who wants Python, a virtual environment, and an empty SQLite file before any data script runs. What you need from this page is the PATH checkbox, the venv, the directory layout, and a table that rejects a duplicate bar.
Who it is not for: Someone who wants the machine on all night, the whole A-share universe, or a commercial data contract. A machine that must stay awake starts with the cloud server note. Filling the table from a short watchlist is the next note. Pulling every listed name is the AKShare database walkthrough. This page is also not the tool-order guide and not the site FAQ. Do not put the database file in OneDrive or any synced folder. The sync client locks the file and writes fail.
The same walkthrough in Chinese is a local Python environment and the first SQLite file. A machine that stays on, including the first SSH login, is opening a cloud server. The same article in Chinese is the Chinese cloud server note. That note is not this article. The next part of this series, a short watchlist written into the table you create here, is the local watchlist sync. The same article in Chinese is the Chinese watchlist sync. That article is not this one either. Storing the whole market is AKShare into SQLite. That walkthrough is a different job.
Facts below follow the Chinese walkthrough. The commands were not re-run for this English page. This is not investment advice. Software versions follow the publisher's current release.
01 · Local or cloud
Use the computer in front of you when all three of these are true:
- The job runs after the close. You are not asking a laptop to wake itself at 2 a.m.
- The bars should stay on that machine. Nothing in this series uploads the database.
- The budget is zero. Python, SQLite, and DB Browser for SQLite are free.
The tradeoff is concrete. The PC has to be on, and sleep interrupts the later scheduled tasks. A review you run yourself after the close is unaffected. If you want the machine up when the lid is shut, leave this path and start with opening a cloud server.
02 · Install Python, and do not skip the checkbox
- Windows. Download the 3.12 or 3.13 installer from python.org. On the first screen, tick Add python.exe to PATH. If that box is clear, every later
pythoncommand fails to resolve. Skip the Microsoft Store build. Its virtual environments misbehave. - macOS. Homebrew:
brew install python.
Open a terminal and check the version. If downloads from PyPI stall on a connection in mainland China, set the index the Chinese page uses. Then give this project its own environment:
python --version # 3.12 or newer means the install worked
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# A project-local environment, so this stack does not share packages
cd D:/quant && python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS: source .venv/bin/activate
pip install akshare pandasThe prompt should show (.venv) before the install. akshare and pandas are not used on this page. The next note imports both.
03 · SQLite: a database you do not install
SQLite is one file. Python already includes the driver, so there is no service to start and no port to open. For a personal price history, that is the right default. Add a viewer if you want to look at the rows without writing SQL:
- DB Browser for SQLite, free and open source. Double-click the
.dbfile and browse the table. - From the terminal,
import sqlite3is enough for this page.
04 · The project directory and the first table
D:/quant/ # project root
├── .venv/ # virtual environment
├── scripts/ # scripts for the later notes
├── logs/ # one log per scheduled job
├── briefs/ # archived review notes (part 3)
└── astock.db # the market database, one SQLite fileimport sqlite3
conn = sqlite3.connect("D:/quant/astock.db")
conn.execute("""
CREATE TABLE IF NOT EXISTS daily(
code TEXT, date TEXT,
open REAL, high REAL, low REAL, close REAL, volume REAL,
PRIMARY KEY(code, date)
)""")
conn.close()PRIMARY KEY(code, date) allows one bar per name per day. A repeated insert raises an error. That is the guard the next note depends on when it skips a row it already stored. CREATE TABLE IF NOT EXISTS means you can run the snippet again without dropping the file.
05 · Check: ten lines through the whole path
import sqlite3
conn = sqlite3.connect("D:/quant/astock.db")
conn.execute("INSERT INTO daily VALUES('600519','2025-08-29',1488,1498,1476,1490,23345)")
print(conn.execute("SELECT * FROM daily").fetchall())
conn.commit(); conn.close()
# The printed row is the one just inserted. Python and SQLite are connected.Run it with the virtual environment active. On Windows, python -X utf8 avoids a garbled console. Open astock.db in DB Browser for SQLite if you want to see the same row outside the terminal. The numbers are a sample row from the Chinese page, not a live quote.
06 · Pitfalls
- Spaces or non-ASCII characters in the path. Some libraries handle them badly. Keep the project root short and plain, such as
D:/quant. - The .db file inside OneDrive or another synced folder. The sync client locks the file and writes fail. Leave the database in a local directory.
- The virtual environment was not activated.
No module named aksharealmost always means the prompt is missing(.venv). - Garbled text in the Windows console. Run the script as
python -X utf8.
The file is empty except for the sample row. The next note fills it after the close:
Next: daily bars for a watchlist, including after a missed boot →
This is not investment advice. Software versions follow the publisher's current release.
Frequently asked questions
How to choose a local Python and SQLite base for an after-close review?
Stay on the computer you already use for review when the job only has to run after the close. The stack costs nothing, the bars never leave the machine, and they sit next to the charting app you already open. The cost is that the PC has to be awake. A sleeping laptop will not run the later tasks. If the machine must stay up overnight, start with the cloud server note instead. For a daily review after the close, local is enough. On Windows, install Python 3.12 or 3.13 from python.org and tick Add python.exe to PATH on the first screen. Skip the Microsoft Store build. Its virtual environments misbehave. On macOS, Homebrew is enough: brew install python. Confirm with python --version. If pip is slow from mainland China, point it at the Tsinghua index the Chinese page uses. Then create D:/quant/.venv so this stack does not share packages with anything else, activate it, and install akshare and pandas into it. SQLite needs no separate server. Python's sqlite3 module opens one file, D:/quant/astock.db. DB Browser for SQLite is the free viewer when you want to see the rows. The first table is daily: code, date, open, high, low, close, volume, with a primary key on code and date, so the same bar cannot be stored twice. Prove the path by inserting 600519 on 2025-08-29 and printing that row. This page does not download a watchlist, does not schedule a task, and does not call a model. Those are later notes. It is not the tool-order guide and not the site FAQ.
Who it is for?
Someone setting up a China A-share review on their own PC, who wants Python, a virtual environment, and an empty SQLite file before any data script runs. What you need from this page is the PATH checkbox, the venv, the directory layout, and a table that rejects a duplicate bar.
Who it is not for?
Someone who wants the machine on all night, the whole A-share universe, or a commercial data contract. A machine that must stay awake starts with the cloud server note. Filling the table from a short watchlist is the next note. Pulling every listed name is the AKShare database walkthrough. This page is also not the tool-order guide and not the site FAQ. Do not put the database file in OneDrive or any synced folder. The sync client locks the file and writes fail.
How do you install Python on Windows and on macOS?
Windows: download the 3.12 or 3.13 installer from python.org. On the first screen, tick Add python.exe to PATH. If you skip it, later python commands are not found. Do not use the Microsoft Store build. Its virtual environments misbehave. macOS: brew install python. Then run python --version. A printout of 3.12 or newer means the install worked. If pip is slow from mainland China, run pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple. Create the project environment with cd D:/quant && python -m venv .venv. On Windows activate it with .venv\Scripts\activate. On macOS use source .venv/bin/activate. The prompt should show (.venv) before you run pip install akshare pandas. A plain path such as D:/quant avoids libraries that mishandle spaces or non-ASCII folders.
Do you install SQLite separately?
No. SQLite is a single file, and Python already ships the sqlite3 driver. There is no server process and no extra installer. The database for this series is D:/quant/astock.db. Install DB Browser for SQLite if you want to double-click the file and look at the table. From Python, import sqlite3 is enough for this page. Keep the file in a local folder. OneDrive and other sync clients lock the file and the next write fails.
What does the first table look like?
One table, named daily. Columns are code TEXT, date TEXT, open REAL, high REAL, low REAL, close REAL, and volume REAL. PRIMARY KEY(code, date) means one bar per name per day. A second insert of the same pair raises an error, which is the idempotent guard the later scripts rely on. Create it with CREATE TABLE IF NOT EXISTS so running the snippet again does not wipe the file. The project layout around that file is D:/quant with .venv, scripts, logs, and briefs. Later notes put scripts in scripts, logs in logs, and review notes in briefs.
How do you prove Python and SQLite are connected?
With the virtual environment active, run a short script against D:/quant/astock.db. Insert one row for 600519 on 2025-08-29 with open 1488, high 1498, low 1476, close 1490, and volume 23345. Then SELECT * FROM daily, commit, and close. The printed row is the one you just inserted. That is the whole path: the interpreter, the file, and the table. On Windows, if the console garbles text, run the script as python -X utf8. If Python says No module named akshare, the prompt is missing (.venv). Activate the environment and try again.
What pitfalls does this walkthrough call out?
Four. A project path with spaces or non-ASCII characters breaks some libraries. Keep the root short and plain, such as D:/quant. A .db file inside OneDrive or another synced folder gets locked and writes fail. Leave the database in a local directory. Forgetting to activate the virtual environment shows up as No module named akshare. Look for (.venv) before the prompt. Windows console encoding is fixed by python -X utf8. This page does not download market data and does not schedule anything. This is not investment advice. Software versions follow the publisher's current release.