#!/usr/bin/env python# Coding=utf-8import sqlite3conn = Sqlite3.connect (": Memory:") C = conn.cursor () c.execute ( ' ' ' CREATE TABLE Stocks (data Text,trans text,symbol test,qty real,price Real) ' C.execute ("INSERT into Stocks VALUES (' 2006 -01-05 ', ' BUY ', ' rhat ', 100,35.14) ') conn.commit () symbol = ' rhat ' C.execute ("SELECT * from Stocks WHERE symbol= '%s '"% Symbol) print c.fetchone () parament = [ (' 2014-11-11 ', ' wcs ', ' Rhat ', $, 20.5), (' 2014-11-15 ', ' wcs ', ' rhat ', 30.0)]# for T in parament:# c.execute (' INSERT INTO stocks VALUES (?,?,?,?,?) ', T) c.executemany ("INSERT INTO stock S VALUES (?,?,?,?,?) ", parament) C.execute (" SELECT * from Stocks WHERE symbol= '%s ' "% symbol) print C.fetchall (
Python is a lightweight development database that is simpler and more intuitive compared to SQL Server and roacle, such as Oracle's huge installation file SQLite is a lot lighter, Take Python. When you install Python, you automatically install SQLite, and users simply load it into the project via import and use it directly.
When you call SQLite, you need to load the Sqlite3 via import, and when you are finished loading it, you can directly call its methods to perform the corresponding operations on the database.
SQLite has two modes of loading:
1 Memory Form
All of the data under this type is stored in memory, which is equivalent to creating a dedicated area in memory when the user runs the program, and automatically emptying all data in its area when the project is finished, and faster access relative to the file.
When you call the. Connect () method, you can create a memory database simply by calling ": Memory:" As a parameter call
2. File Form
. Connect ("filename") is automatically loaded when the system already has a database of the corresponding name, and is created automatically when there is no database.
If you need to perform the appropriate data operation, you need to call its. Execute () method
1. Build table operation
c.execute(
‘‘‘CREATE TABLE stocks(data text,trans text,symbol test,qty real,price real)‘‘‘)
The above statement is a SQL statement that creates a table with the table named stocks
2. Insert operation
2.1: Using tuples to implement multiple data insertions at the same time
Parament = [(' 2014-11-11 ', ' wcs ', ' Rhat ', Max, 20.5), (' 2014-11-15 ', ' wcs ', ' rhat ', ' 30.0 ')]for T in Parament: C.execute ("INSERT into stocks VALUES (?,?,?,?,?)", T) c.execute ("SELECT * from Stocks WHERE symbol= '%s '"% symbol) Print C. Fetchall ()
(U ' 2006-01-05 ', U ' BUY ', U ' rhat ', 100.0, 35.14)
[(U ' 2006-01-05 ', U ' BUY ', U ' rhat ', 100.0, 35.14), (U ' 2014-11-11 ', U ' wcs ', U ' rhat ', 200.0, 20.5), (U ' 2014-11-15 ', U ' wcs ', U ' Rhat ', 300.0, 30.0)]
The first one is the initial state, and the second one inserts the subsequent query
2.2 Inserting multiple statements with Executemany
Parament = [(' 2014-11-11 ', ' wcs ', ' Rhat ', $, 20.5), (' 2014-11-15 ', ' wcs ', ' rhat ', 300, 30.0)]
c.executemany("INSERT INTO stocks VALUES(?,?,?,?,?)", parament)
c.execute("SELECT * FROM stocks WHERE symbol=‘%s‘" % symbol)
print c.fetchall(
The result is the same as 2.1
Python SQLite (1)