Mysql database learning notes (3)

xiaoxiao2021-03-06  104

Appendix a mytest.c

/ ************************************************** *************** /

/ * AUTHOR: Allen Lee TS: 2 / 4/2004 9:15 * /

/ * Date: 2 / 4/2004 * /

/ * Mytest.c: test ddl stat * /

/ * * /

/ * History: * /

/ * * /

/ ************************************************** *************** /

#include

#include

#include

#include

#define string_size 50

#define drop_sample_table "DROP TABLE IF EXISTS TEST_TABLE"

#define create_sample_table "create Table Test_Table (col1 int, / /") (CREATE TABLE TEST_TABLE (Col1 Int, /

COL2 VARCHAR (40), /

COL3 Smallint, /

Col4 TimeStamp) "

#define insert_sample "INSERT INTO TEST_TABLE (COL1, COL2, COL3) VALUES (?,?,?)

INT main (int Argc, char ** argv) {

Mysql * mysql;

mysql = mysql_init (null);

IF (! mysql_real_connect (mysql, "localhost", "root", ",", 3306, ", null) {/ * make connection with mysql_connect (mysql db, char * host, char * username, char * PASSWORD * /

Printf (MySQL_ERROR (mysql));

Exit (1);

}

IF (mysql_select_db (mysql, "test")) {/ * Select the Database We want to use * /

Printf (MySQL_ERROR (mysql));

Exit (1);

}

Mysql_stmt * stmt;

MySQL_BIND BIND [3];

MY_ULONGLONG Affected_Rows;

Int param_count;

Short small_data;

int tent_data;

Char str_data [string_size];

UNSIGNED Long Str_Length

MY_BOOL IS_NULL;

IF (MySQL_Query (mysql, drop_sample_table)

{

FPRINTF (stderr, "drop table filed / n");

FPRINTF (stderr, "% s / n", mysql_error (mysql));

exit (0);

}

IF (MySQL_Query (mysql, create_sample_table) {

FPRINTF (stderr, "create table filed / n");

FPRINTF (stderr, "% s / n", mysql_error (mysql));

exit (0);

}

/ * Prepare an insert query with 3 parameters * /

/ * (The TimeStamp Column Is Not Named; It Will * /

/ * be set to the current date and time) * /

STMT = mysql_prepare (mysql, insert_sample, strlen);

IF (! STMT)

{

FPrintf (stderr, "mysql_prepare (), insert filed / n");

FPRINTF (stderr, "% s / n", mysql_error (mysql));

exit (0);

}

FPRINTF (stdout, "prepare, insert serviceful / n");

/ * Get the parameter count from the statement * /

PARAM_COUNT = mysql_param_count (stmt);

FPrintf (stdout, "total parameters in insert:% d / n", param_count);

IF (param_count! = 3) / * validate parameter count * /

{

FPRINTF (stderr, "invalid parameter count returned by mysql / n");

exit (0);

}

/ * Bind The Data for ALL 3 parameters * /

/ * Integer param * /

/ * This is a number type, sored is no need to specify buffer_length * /

BIND [0] .buffer_type = mysql_type_long;

BIND [0] .buffer = (char *) & int_data;

BIND [0] .is_null = 0;

Bind [0] .length = 0;

/ * String param * /

BIND [1] .buffer_type = mysql_type_var_string;

BIND [1] .buffer = (char *) STR_DATA;

BIND [1] .buffer_length = string_size;

BIND [1] .is_null = 0;

BIND [1] .length = & str_length;

/ * Smallint param * /

BIND [2] .buffer_type = mysql_type_sh;

BIND [2] .buffer = (char *) & small_data;

BIND [2] .is_null = & is_null;

BIND [2] .length = 0;

/ * Bind the buffers * /

IF (MySQL_BIND_PARAM (STMT, BIND))

{

FPRINTF (stderr, "mysql_bind_param () failed / n");

FPRINTF (stderr, "% s / n", mysql_stmt_error (stmt));

exit (0);

}

/ * Specify the data value for the first row * /

INT_DATA = 10; / * integer * /

Strncpy (str_data, "mysql", string_size); / * String * /

Str_Length = Strlen (Str_Data);

/ * Insert Smallint Data as Null * /

IS_NULL = 1;

/ * EXECUTE the insert statement - 1 * /

IF (MySQL_EXECUTE (STMT))

{

FPRINTF (stderr, "mysql_execute (), 1 failed / n");

FPRINTF (stderr, "% s / n", mysql_stmt_error (stmt));

exit (0);

}

/ * Get the total number of affected rows * /

Affected_rows = mysql_stmt_affected_rows (stmt);

FPRINTF (stdout, "Total Affected Rows (Insert 1):% LD / N", Affected_Rows);

IF (Affected_Rows! = 1) / * Validate affected rows * /

{

FPRINTF (stderr, "invalid affected rows by mysql / n");

exit (0);

}

/ * Specify Data Values ​​for Second Row, Then Re-Execute The Statement * /

INT_DATA = 1000;

STRNCPY (Str_Data, "The Most Popular Open Source Database", String_size);

Str_Length = Strlen (Str_Data);

Small_data = 1000; / * smallint * /

IS_NULL = 0; / * reset * /

/ * EXECUTE THE INSERT STATEMENT - 2 * /

IF (MySQL_EXECUTE (STMT))

{

FPRINTF (stderr, "mysql_execute, 2 failed / n");

FPRINTF (stderr, "% s / n", mysql_stmt_error (stmt));

exit (0);

}

/ * Get the total rows affected * /

Affected_rows = mysql_stmt_affected_rows (stmt);

FPRINTF (stdout, "Total Affected Rows (Insert 2):% LD / N", Affected_Rows);

IF (Affected_Rows! = 1) / * Validate affected rows * /

{

FPRINTF (stderr, "invalid affected rows by mysql / n");

exit (0);

}

/ * Close the statement * /

IF (MySQL_STMT_Close (STMT))

{

FPRINTF (stderr, "failed while closing the statement / n");

FPRINTF (stderr, "% s / n", mysql_stmt_error (stmt));

exit (0);

}

MySQL_Close (mysql);

Return 0;

}

Appendix II CREATEDB.CPP

#include

#include

#include

INT Main (int Argc, char * argv []) {

Connection connection (use_exception);

Try {// The entire main block is one big try block

IF (argc == 1) Connection.connect ("mysql_cpp_data", "localhost", "root", "");

Else if (argc == 2) Connection.connect ("", Argv [1]);

Else if (argc == 3) Connection.Connect ("", Argv [1], Argv [2]);

ELSE IF (Argc <= 4) Connection.connect ("", Argv [1], Argv [2], Argv [3]);

// Create a new Object and connect based on any (if any) arguments

// passed to main ();

Try {

Connection.select_db ("MySQL_CPP_DATA);

} catch (badquery ER) {

// if it couldn't connect to the database assume That doesn't exist

// and try created it. if That Does Not Work Exit with an error.

Connection.create_db ("MySQL_CPP_DATA);

Connection.select_db ("MySQL_CPP_DATA);

}

Query Query = connection.Query (); // Create a new query Object

Try {// ignore Any Errors Here

// i hope to make this Simpler Soon

Query.execute ("DROP TABLE Stock");

} catch (badquery ER) {}

Query << "Create Table Stock (Item Char (20) Not Null, Num Bigint,"

<< "Weight Double, Price Double, SDATE DATE";

Query.execute (Reset_Query);

// send the query to create the table and execute it. The TABLE AND EXECUTE.

// reset_Query Tells the Query Object to Reset IT Self After

// Execution

Query << "INSERT INTO% 5: Table VALUES (% 0Q,% 1Q,% 2,% 3,% 4Q)";

Query.Parse ();

// set up the template query i will use to insert the data. The THE

// PARSE METHOD CALL IS IMPORTANT AS IS What Lets The Query // Know That this Is A Template and not a literal string

Query.def ["Table"] = "stock";

// this is setting the parameter named Table to stock.

Query.execute ("Hamburger Buns", 56, 1.25, 1.1, "1998-04-26");

Query.execute ("HotDogs' Buns", 65, 1.1, 1.1, "1998-04-23");

Query.execute ("DINNER ROLES", 75, .95, .97, "1998-05-25");

Query.execute ("Allen Lee", 87, 1.5, 1.75, "1998-09-04");

// The last parameter "Table" is not specific here. Thus

// The default value for "Table" is buy which is "stock".

} catch (badquery ER) {// handle Any Errors That May Come Up

CERR << "Error:" << Er.error << Endl;

Return -1;

}

}

Appendix three fieldInf.cpp

#include

#include

#include

#include

Int main () {

Try {// ITS in One Big Try Block

Connection Con (Use_exceptions);

Con.Connect ("MySQL_CPP_DATA", "LocalHost", "root", "");

Query query = con.query ();

Query << "Select * from stock";

Result res = query.store ();

Cout << "Query:" << query.preview () << endl;

Cout << "Records Found:" << res.size () << Endl << endl;

COUT << "Query Info: / N";

Cout.setf (ios :: left);

For (unsigned int i = 0; i

Cout << SETW (2) << i

<< SETW (15) << res.names (i) .c_str ()

// this is the name of the field

<< SETW (15) << res. Types (i). SQL_NAME ()

// this is the SQL Identifier Name // Result :: Types (unsigned int) Returns a mysql_type_info Which in Many

// Ways is like type_info except That It has additional SQL TYPE

// Information in it. (with one of the methods being sql_name ())

<< SETW (20) << Res.Types (i) .Name ()

// this is The C Identifier Name Which Most Closely Rembly

// the SQL Name (ITS Is Implementation Defined And Offe Very Readable)

<< ENDL;

}

Cout << Endl;

IF (res.types (0) == TypeId (string))

COUT << "Field 'Item' Is of An SQL Type Which Most Closely Rembly A / N"

<< "The C String Type / N";

// this Is Demonstrating How A MySQL_TYPE_INFO CAN BE Compared with a C

// Type_info.

IF (res.types (1) == TypeId (longlong))

Cout << "Field 'Num' Is of An Sql Type Which Most Closely Rembly A / N"

<< "THE C long long int type / n";

Else if (res. types (1) .BASE_TYPE () == TypeId (longlong))

COUT << "Field 'Num' Base Type is of an SQL TYPE Which MOST Closely / N"

<< "" Resembles A THE C long long int type / n ";

//Howe you have to be be a careful as if it can be null the actual type is

// Null not type. So you shop always use the base_type method

// to get at the underlying type. if the Type is not null Than this base

// Type Would Be The Same As ITS Type.

Return 0;

} catch (badquery ER) {

CERR << "Error:" << Er.error << Endl;

Return -1;

} Catch (Badconversion ER) {// Handle Bad Conversions

CERR << "Error: Tried to Convert /" "<< Er.Data <<" / "to a /" "

<< Er.TYPE_NAME << "/". "<< Endl; Return -1;

}

}

转载请注明原文地址:https://www.9cbs.com/read-97408.html

New Post(0)