跳轉到內容

Ring/教程/ODBC 函式

來自華夏公益教科書,自由的教學讀物

ODBC 函式

[編輯 | 編輯原始碼]

本章包含 Ring 程式語言提供的 ODBC 函式。在使用這些函式之前,請載入 odbclib.ring 庫。

	load "odbclib.ring"
  • odbc_init()
  • odbc_drivers()
  • odbc_datasources()
  • odbc_close()
  • odbc_connect()
  • odbc_disconnect()
  • odbc_execute()
  • odbc_colcount()
  • odbc_fetch()
  • odbc_getdata()
  • odbc_tables()
  • odbc_columns()
  • odbc_autocommit()
  • odbc_commit()
  • odbc_rollback()

odbc_init() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_init() 函式建立 ODBC 控制代碼。

語法

	odbc_init() ---> ODBC Handle

odbc_drivers() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_drivers() 函式獲取 ODBC 驅動程式列表。

語法

	odbc_drivers(ODBC Handle) ---> List of Drivers

odbc_datasources() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_datasources() 函式獲取 ODBC 資料來源列表。

語法

	odbc_datasources(ODBC Handle) ---> List of Data sources

odbc_close() 函式

[編輯 | 編輯原始碼]

在使用完 ODBC 函式後,我們可以使用 ODBC_Close() 函式釋放資源。

語法

	odbc_close(ODBC Handle)
[編輯 | 編輯原始碼]

以下示例列印 ODBC 驅動程式列表。

	See "ODBC test 1" + nl
	oODBC = odbc_init()
	See "Drivers " + nl
	see odbc_drivers(oODBC)
	odbc_close(oODBC)

輸出

	ODBC test 1
	Drivers
	Microsoft Access-Treiber (*.mdb) - SQLLevel=0
	Driver do Microsoft Paradox (*.db ) - SQLLevel=0
	Driver do Microsoft Excel(*.xls) - SQLLevel=0
	Microsoft Text Driver (*.txt; *.csv) - SQLLevel=0
	Driver da Microsoft para arquivos texto (*.txt; *.csv) - SQLLevel=0
	Microsoft dBase-Treiber (*.dbf) - SQLLevel=0
	SQL Server - CPTimeout=60
	Microsoft Excel Driver (*.xls) - SQLLevel=0
	Driver do Microsoft dBase (*.dbf) - SQLLevel=0
	Microsoft Paradox-Treiber (*.db ) - SQLLevel=0
	Microsoft ODBC for Oracle - CPTimeout=120
	Microsoft Text-Treiber (*.txt; *.csv) - SQLLevel=0
	Microsoft Excel-Treiber (*.xls) - SQLLevel=0
	Microsoft Access Driver (*.mdb) - SQLLevel=0
	Driver do Microsoft Access (*.mdb) - SQLLevel=0
	Microsoft Paradox Driver (*.db ) - SQLLevel=0
	Microsoft dBase Driver (*.dbf) - SQLLevel=0
	Microsoft Access Driver (*.mdb, *.accdb) - UsageCount=3
	Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) - UsageCount=3
	Microsoft Access Text Driver (*.txt, *.csv) - UsageCount=3
	SQL Server Native Client 10.0 - UsageCount=1
	SQL Server Native Client 11.0 - UsageCount=1
	Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx) - UsageCount=3
	Microsoft Access Paradox Driver (*.db) - UsageCount=3
	MySQL ODBC 5.3 ANSI Driver - UsageCount=1
	MySQL ODBC 5.3 Unicode Driver - UsageCount=1
	ODBC Driver 11 for SQL Server - UsageCount=1
	Lianja ODBC Driver - CPTimeout=60
	Microsoft Visual FoxPro Driver - UsageCount=1
	Microsoft Visual FoxPro-Treiber - UsageCount=1
	Driver para o Microsoft Visual FoxPro - UsageCount=1
	Microsoft FoxPro VFP Driver (*.dbf) - UsageCount=1
[編輯 | 編輯原始碼]

以下示例列印 ODBC 資料來源列表。

	See "ODBC test 2" + nl
	pODBC = odbc_init()
	See "Data Sources " + nl
	see odbc_datasources(pODBC)
	odbc_close(pODBC)

輸出

	ODBC test 2
	Data Sources
	Excel Files - Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)
	MS Access Database - Microsoft Access Driver (*.mdb, *.accdb)
	Customer - Microsoft Access Driver (*.mdb)
	IdCardData - Microsoft Access Driver (*.mdb)
	MyProjectData2 - Microsoft Access Driver (*.mdb)
	MyData - Microsoft Access Driver (*.mdb)
	MonprojetData - Microsoft Access Driver (*.mdb)
	dBASE Files - Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)
	myvfpdata - Microsoft Visual FoxPro Driver
	FACTORYDATA - Microsoft Access Driver (*.mdb)
	TRAININGSYSDATA - Microsoft Access Driver (*.mdb)
	RVCSYSDATASQLDB - SQL Server Native Client 11.0
	PWCTRVCDATA - Microsoft Access Driver (*.mdb)
	MyCompany - Microsoft Access Driver (*.mdb)
	HCS - Microsoft Access Driver (*.mdb)
	HCS2 - Microsoft Access Driver (*.mdb, *.accdb)
	MyProjectData - Microsoft Access Driver (*.mdb)
	Xtreme Sample Database 2008 - Microsoft Access Driver (*.mdb)
	Lianja_Southwind - Lianja ODBC Driver
	Visual FoxPro Database - Microsoft Visual FoxPro Driver
	Visual FoxPro Tables - Microsoft Visual FoxPro Driver

odbc_connect() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_connect() 函式連線到資料庫。

語法

	odbc_connect(ODBC Handle, cConnectionString)

odbc_disconnect() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_disconnect() 函式關閉到資料庫的連線。

語法

	odbc_disconnect(ODBC Handle)

開啟和關閉連線

[編輯 | 編輯原始碼]

以下示例連線到資料庫,然後關閉連線。

	See "ODBC test 3" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	see odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	See "disconnect" + nl
	odbc_disconnect(pODBC)
	See "Close database..." + nl
	odbc_close(pODBC)

輸出

	ODBC test 3
	Connect to database
	1
	disconnect
	Close database...

odbc_execute() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_execute() 函式在資料庫上執行 SQL 語句。

語法

	odbc_execute(ODBC Handle, cSQLStatement)


odbc_colcount() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_colcount() 函式獲取查詢結果中的列數。

語法

	odbc_colcount(ODBC Handle) ---> Columns Count as Number

odbc_fetch() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_fetch() 函式從查詢結果中獲取一行。

語法

	odbc_fetch(ODBC Handle)

odbc_getdata() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_getdata() 函式從獲取的行中獲取列值。

語法

	odbc_getdata(ODBC Handle, nColumnNumber) ---> Column Value

執行查詢並列印結果

[編輯 | 編輯原始碼]

以下示例執行查詢,然後列印查詢結果。

	See "ODBC test 4" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	see odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	See "Select data" + nl
	see odbc_execute(pODBC,"select * from person") + nl
	nMax = odbc_colcount(pODBC)
	See "Columns Count : " + nMax + nl
	while odbc_fetch(pODBC)
		See "Row data:" + nl
		for x = 1 to nMax
			see odbc_getdata(pODBC,x) + " - "
		next
	end
	See "Close database..." + nl
	odbc_disconnect(pODBC)
	odbc_close(pODBC)

odbc_tables() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_tables() 函式獲取資料庫中的表列表。

我們可以像獲取任何查詢結果一樣訪問此函式的結果。

語法

	odbc_tables(ODBC Handle)

示例

	See "ODBC test - Get Database Tables" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	See "Select data" + nl
	odbc_tables(pODBC) + nl
	nMax = odbc_colcount(pODBC)
	See "Columns Count : " + nMax + nl
	while odbc_fetch(pODBC)
		for x = 1 to nMax
			see odbc_getdata(pODBC,x) 
			if x != nMax see " - " ok
		next
		See nl
	end
	See "Close database..." 
	odbc_disconnect(pODBC)
	odbc_close(pODBC)

輸出

	ODBC test - Get Database Tables
	Connect to database
	Select data
	Columns Count : 5
	.\test - NULL - Customer - TABLE - NULL
	.\test - NULL - employee - TABLE - NULL
	.\test - NULL - person - TABLE - NULL
	.\test - NULL - tel - TABLE - NULL
	Close database...


odbc_columns() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_columns() 函式獲取表中的列列表。

語法

	odbc_columns(ODBC Handle, cTableName)

示例

	See "ODBC test - Get Table Columns" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	See "Get Columns inside the Person Table" + nl
	odbc_columns(pODBC,"person") + nl
	while odbc_fetch(pODBC)
		see odbc_getdata(pODBC,4) + nl
	end
	See "Close database..." + nl
	odbc_disconnect(pODBC)
	odbc_close(pODBC)

輸出

	ODBC test - Get Table Columns
	Connect to database
	Get Columns inside the Person Table
	FIRST
	LAST
	STREET
	CITY
	STATE
	ZIP
	HIREDATE
	MARRIED
	AGE
	SALARY
	NOTES
	Close database...

odbc_autocommit() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_autocommit() 函式啟用或停用自動提交功能。

語法

	odbc_autocommit(ODBC Handle, lStatus)   # lStatus can be True or False


odbc_commit() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_commit() 函式將更新提交到資料庫。

語法

	odbc_commit(ODBC Handle)

odbc_rollback() 函式

[編輯 | 編輯原始碼]

我們可以使用 odbc_rollback() 函式回滾對資料庫的更新。

語法

	odbc_rollback(ODBC Handle)

事務和使用提交和回滾

[編輯 | 編輯原始碼]

示例

	See "ODBC Test - Transactions and using Commit and Rollback" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	see odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	see "insert data..." + nl
	odbc_autocommit(pODBC,0)
	for x = 1 to 10000
		odbc_execute(pODBC,"insert into tel values (" + x + ",'mahmoud')")
	next
	for x = 10001 to 15000
		odbc_execute(pODBC,"insert into tel values (" + x + ",'samir')")
	next
	odbc_commit(pODBC)

for x = 15001 to 20000 odbc_execute(pODBC,"insert into tel values (" + x + ",'fayed')") next

ODBC_ROLLBACK(pODBC) odbc_execute(pODBC,"insert into tel values (" + x + ",'fayed')") odbc_commit(pODBC)

See "Close database..." + nl odbc_disconnect(pODBC) odbc_close(pODBC)

輸出

	ODBC Test - Transactions and using Commit and Rollback
	Connect to database
	1
	insert data...
	Close database...

儲存和恢復影像

[編輯 | 編輯原始碼]

下一個示例將影像儲存到資料庫中。

	See "ODBC test - Save image in the database" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	see odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	see "Read Image File..." + nl
	cFile = str2hex(read("tests\mahmoud.jpg"))
	see "size " + len(CFile)+nl
	see "Save image in the database..." + nl
	stmt = "insert into tel values (20000,'mahmoud','" + cFile + "');"
	odbc_execute(pODBC,stmt)
	See "Close database..." + nl
	odbc_disconnect(pODBC)
	odbc_close(pODBC)

下一個示例從資料庫中恢復影像。

	See "ODBC Test - Restore image from the database" + nl
	pODBC = odbc_init()
	See "Connect to database" + nl
	see odbc_connect(pODBC,"DBQ=test.mdb;Driver={Microsoft Access Driver (*.mdb)}") + nl
	See "Select data" + nl
	see odbc_execute(pODBC,"select * from tel") + nl
	nMax = odbc_colcount(pODBC)
	See "Columns Count : " + nMax + nl
	while odbc_fetch(pODBC)
		See "Write image file" + nl
		write("tests\great.jpg",hex2str( odbc_getdata(pODBC,3) ) )
	end
	See "Close database..." + nl
	odbc_disconnect(pODBC)
	odbc_close(pODBC)


華夏公益教科書