This article describes the SQL package principle and usage of go. Share to everyone for your reference, specific as follows:
The go SQL package is in Pkg/database, where the two packages of SQL and Sql/driver can be seen together. It is recommended that you look at the doc.txt in the SQL folder before looking at this two package. This document says a few more important things:
1 These two packages are really go-style packages.
2 This use of these two packages does not need to be about concurrent processing, and do not need to maintain their own database connection pool, once a connection has been established, this connection can be shared between the various goroutine.
3 Sql/driver provides the database interface, the concrete implementation also needs to realize itself.
First look at the Database/driver bag.
The first method to use is the register
This method will achieve the driver. Driver drives are registered into the variable drivers, and when a driver is written, it is necessary to register the driver in SQL to use these interfaces in the SQL package. This realizes the driver. The driver must implement the Open method.
Driver. Open returns the Driver.conn, it's three methods
Prepare: Parameter Bindings
Close: Turn off connection
Begin: Supporting Transactions
First look at prepare, this is the same usage as PHP MySQL PDO
Conn.prepare ("SELECT * from Test where a=?")
STMT structure to return:
Close: Turn off this statement
Numinput: Returns how many parameters can be bound
Use of Exec:insert or update without return
Query:select and other query operations using
exec is a bound variable, and then returns the result structure
Query is a bound variable, and then returns the rows result set
Look at the method in result:
Lastinsertid (): The primary key ID obtained after the insert operation
Rowsaffect (): Number of rows affected
Rows:
Columns (): Which columns are returned with the data, which is actually the returned table column name
Close (): Rows are closed and no action can be made after the call
Next (): Takes the next line of data to des[] value. The value interface here can be int64,float64,bool,[]byte,string,time. Time
The following goes back to begin and returns the TX
After starting a transaction, in addition to the query, there are two kinds of behavior: Commit and rollback, both of which are methods of the TX interface
The structure in the Drvier is all interface-type, it needs you to implement and register in the register.
The specific use of the drive is in the Database/sql
First, look at the structure of a few SQL
First, the structure in SQL is encapsulated in the structure of the driver, like rows, where an internal attribute is Rowsi driver. Rows.
The actual operation of SQL is to use driver. Rows inside the interface to operate, is actually using your own implementation of the driver to operate.
Driver and SQL are like plugs and a car full of plugs, you realize the driver, that is, the configuration of these plugs, you can use the SQL of this car.
Result: Consistent with the result in driver, that is, if you achieve driver. result, you naturally implement SQL. Result. It is an interface, in fact, there is no special use, if the SQL package all result is replaced by Driver.result is also a row, it is estimated that the author wants to return the value do not refer to other packages, use this method.
Rows: Based on the driver. Rows, and there are several other ways to extend the above. Own method:
Close
Cloumns
Err
Next
Scan
STMT: Based on driver. Stmt. Own method
Close
Exec
Query
Queryrow
Tx: Based on DRIVER.TX. Own method:
Commit
Exec
Prepare
Query
Queryrow
Rollback
Stmt
From SQL. Open start
Returned the SQL. DB structure, this structure is implemented driver. Conn structure, in addition to conn existing prepare,begin,close, but also a number of direct query methods:
Driver (): Back to current drive
Exec (): direct operation
Query (): For queries, return rows
Queryrow (): Expected return row, return row
Both rows and row have a very good way to scan data into a set of variables.
For example, the following are typical rows usage
Copy Code code as follows:
Rows, err: = db. Query ("Select ...")
...
For rows. Next () {
var ID int
var name string
Err = rows. Scan (&id, &name)
...
}
Prepare return stmt structure
EXEC returns the result structure
As for these structures, they also have their own methods.
I hope this article will help you with your go language program.