luasql mysql.so_luasql库 说明文档

Introduction

LuaSQL is a simple interface from Lua to a number of database management systems. It includes a set of drivers to some popular databases (currently PostgreSQL, ODBC, MySQL, SQLite, Oracle, and ADO; Interbase and Sybase are in our plans). LuaSQL defines a simple object-oriented API. All drivers should implement this common API, but each one is free to offer extensions.

LuaSQL defines one single global variable, a table called luasql. This table is used to store the initialization methods of the loaded drivers. These methods are used to create an environment object which is used to create a connection object. A connection object can execute SQL statements and eventually create a cursor object which is used to retrieve data.

LuaSQL is free software and uses the same license as Lua 5.1.

Compiling

LuaSQL is distributed as a set of C source files: a pair of common source and header files (luasql.h and luasql.c); and one source file for each driver. Each driver should be compiled with the luasql.c file to generate a library. This library can be linked to the application or dynamically loaded. The initialization function is luaopen_luasqldrivername and it is a Lua open-library compatible function.

Installation

Since version 2.3, all LuaSQL drivers follow the package model of Lua 5.2. All drivers should be "installed" in your package.cpath into a folder called luasql.

In order to use LuaSQL with ADO, make sure that you have installed LuaCOM 1.3 for the appropriate version of Lua.

Error handling

LuaSQL is just an abstraction layer that communicates between Lua and a database system. Therefore errors can occur on both levels, that is, inside the database client or inside LuaSQL driver.

Errors such as malformed SQL statements, unknown table names etc. are called database errors and will be reported by the function/method returning nil followed by the error message provided by the database system. Errors such as wrong parameters, absent connection, invalid objects etc., called API errors, are usually program errors and so will raise a Lua error.

This behavior will be followed by all functions/methods described in this document unless otherwise stated.

Drivers

A LuaSQL driver allows the use of the LuaSQL API with a database management system that corresponds to the driver. To use a driver you have to load it. The example below

local driver = require "luasql.odbc"

loads the ODBC driver and returns a table with an entry with the name of the driver (odbc in this case). Note that you can have more than one driver loaded at the same time doing something like:

local odbc_driver = require "luasql.odbc"

local oci8_driver = require "luasql.oci8"

This example also shows that the driver name not always correspond to the Database name, but to the driver name in the file system. Since it refers to the OCI8 API, the Oracle driver has the name oci8.

Environment Objects

An environment object is created by calling the driver's initialization function that is stored in the table returned when it was loaded, indexed with the same name as the driver (odbc, postgres etc). The following example, will try to create an environment object using the ODBC driver.

local driver = require"luasql.odbc"

local env = driver.odbc()

Methods

env:close()

Closes the environment env. Only successful if all connections pertaining to it were closed first.

Returns: true in case of success; false when the object is already closed.

env:connect(sourcename[,username[,password]])

Connects to a data source specified in sourcename using username and password if they are supplied.

The sourcename may vary according to each driver. Some use a simple database name, like PostgreSQL, MySQL and SQLite; the ODBC driver expects the name of the DSN; the Oracle driver expects the service name; See also: PostgreSQL, and MySQL extensions.

Returns: a connection object.

Connection Objects

A connection object contains specific attributes and parameters of a single data source connection. A connection object is created by calling the environment:connect method.

Methods

conn:close()

Closes the connection conn. Only successful if all cursors pertaining to it have been closed and the connection is still open.

Returns: true in case of success and false in case of failure.

conn:commit()

Commits the current transaction. This feature might not work on database systems that do not implement transactions.

Returns: true in case of success and false when the operation could not be performed or when it is not implemented.

conn:execute(statement)

Executes the given SQL statement.

Returns: a cursor object if there are results, or the number of rows affected by the command otherwise.

conn:rollback()

Rolls back the current transaction. This feature might not work on database systems that do not implement transactions.

Returns: true in case of success and false when the operation could not be performed or when it is not implemented.

conn:setautocommit(boolean)

Turns on or off the "auto commit" mode. This feature might not work on database systems that do not implement transactions. On database systems that do not have the concept of "auto commit mode", but do implement transactions, this mechanism is implemented by the driver.

Returns: true in case of success and false when the operation could not be performed or when it is not implemented.

Cursor Objects

A cursor object contains methods to retrieve data resulting from an executed statement. A cursor object is created by using the connection:execute function. See also PostgreSQL and Oracle extensions.

Methods

cur:close()

Closes this cursor.

Returns: true in case of success and false when the object is already closed.

cur:fetch([table[,modestring]])

Retrieves the next row of results.

If fetch is called without parameters, the results will be returned directly to the caller. If fetch is called with a table, the results will be copied into the table and the changed table will be returned. In this case, an optional modestring parameter can be used. It is just a string indicating how the resulting table should be constructed. The mode string can contain:

"n"

the resulting table will have numerical indices (default)

"a"

the resulting table will have alphanumerical indices

The numerical indices are the positions of the fields in the SELECT statement; the alphanumerical indices are the names of the fields.

The optional table parameter is a table that should be used to store the next row. This allows the use of a unique table for many fetches, which can improve the overall performance.

A call to fetch after the last row has already being returned will close the corresponding cursor.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pdo_mysql.so文件是PHP编程语言中用于连接和操作MySQL数据的一个扩展模块。这个扩展模块可以通过在PHP配置文件中启用来提供对MySQL数据的访问功能。 pdo_mysql.so文件实际上是一个动态链接,它包含了一些函数和类,用于在PHP中进行与MySQL数据的交互。通过加载和使用这个扩展,我们可以使用PDO(PHP数据对象)来进行数据操作,比如执行SQL查询、插入、更新和删除数据等。 使用pdo_mysql.so文件连接MySQL数据具有很多优点。首先,它是PHP官方提供的标准扩展,保证了其稳定性和兼容性。其次,通过PDO进行数据操作可以提供更好的代码可读性和维护性,使得我们可以更方便地编写和调试数据相关的代码。此外,pdo_mysql.so还支持预处理语句和绑定参数,可以有效地防止SQL注入攻击。 要使用pdo_mysql.so文件,我们首先需要在php.ini配置文件中启用该扩展。我们可以通过修改php.ini文件中的相关配置项来启用pdo_mysql.so,并根据需要配置连接MySQL数据的相关参数,如主机名、用户名、密码和数据名等。然后,我们可以在PHP代码中使用PDO类和pdo_mysql.so提供的方法来连接数据、执行SQL语句,并获取结果。 总之,pdo_mysql.so文件是PHP编程语言中用于连接和操作MySQL数据的一个重要扩展模块。通过加载并使用pdo_mysql.so,我们可以使用PDO来进行数据操作,提高代码的可读性和维护性,并提供更好的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值