| Product_Code | Description | Unit_Price |
|---|---|---|
| 116-064 | Toaster | 24.95 |
| 257-535 | Hair dryer | 29.95 |
| 643-119 | Car vacuum | 19.99 |
| SQL Data Type | C++ Data Type |
|---|---|
| INTEGER or INT | int |
| REAL | float |
| DOUBLE | double |
| DECIMAL(n, x) | Fixed-point decimal numbers with n total digits and x digits after the decimal point |
| BOOLEAN | bool |
| CHARACTER(x) or CHAR(x) | Fixed-length string of length x. Similar to string |
CREATE TABLE Products ( Product_Code CHAR(11), Description CHAR(40), Unit_Price DECIMAL(10, 2) )
'Sam''s Small Appliances'
INSERT INTO Products
VALUES ('257-535', 'Hair dryer', 29.95)
DROP TABLE Test
class Customer
{
...
private:
string name;
string address;
string city;
string state;
string zip;
};
| Name | Address | City | State | Zip |
|---|---|---|---|---|
| CHAR(40) | CHAR(40) | CHAR(30) | CHAR(2) | CHAR(10) |
| Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 |
class Invoice
{
...
private:
Customer* cust;
...
};
| Invoice_Number | Customer_ Name |
Customer_ Address |
Customer_ City |
Customer_ State |
Customer_ Zip |
... |
|---|---|---|---|---|---|---|
| INTEGER | CHAR(40) | CHAR(40) | CHAR(30) | CHAR(2) | CHAR(10) | ... |
| 11731 | Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 | ... |
| 11732 | Electronics Unlimited | 1175 Liberty Ave. | Pleasantville | MI | 45066 | ... |
| 11733 | Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 | ... |
|
|
||||||||||||||||||
| Customer | |||||
|---|---|---|---|---|---|
| Customer_ Number |
Name | Address | City | State | Zip |
| INTEGER | CHAR(40) | CHAR(40) | CHAR(30) | CHAR(2) | CHAR(10) |
| 3175 | Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 |
| 3176 | Electronics Unlimited | 1175 Liberty Ave. | Pleasantville | MI | 45066 |
class Invoice
{
...
private:
Customer* cust;
vector<Item> items;
double payment;
};
| Poor design for Invoice Table | ||||||||
|---|---|---|---|---|---|---|---|---|
| Invoice_ Number |
Customer_ Number |
Product_ Code1 |
Quantity1 | Product_ Code2 |
Quantity2 | Product_ Code3 |
Quantity3 | Payment |
| INTEGER | INTEGER | CHAR(10) | INTEGER | CHAR(10) | INTEGER | CHAR(10) | INTEGER | DECIMAL(10,2) |
| 11731 | 3175 | 116-064 | 3 | 257-535 | 1 | 643-119 | 2 | 0 |
|
|
|||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||
| Invoice | ||
|---|---|---|
| Invoice_Number | Customer_Number | Payment |
| INTEGER | INTEGER | DECIMAL(10,2) |
| 11731 | 3175 | 0 |
| 11732 | 3176 | 249.5 |
| 11733 | 3175 | 0 |
| Customer | |||||
|---|---|---|---|---|---|
| Customer_Number | Name | Address | City | State | Zip |
| INTEGER | CHAR(40) | CHAR(40) | CHAR(30) | CHAR(2) | CHAR(10) |
| 3175 | Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 |
| 3176 | Electronics Unlimited | 1175 Liberty Ave. | Pleasantville | MI | 45066 |
SELECT * FROM Customer
| 3175 | Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 |
| 3176 | Electronics Unlimited | 1175 Liberty Ave. | Pleasantville | MI | 45066 |
SELECT City, State FROM Customer
| Anytown | CA |
| Pleasantville | MI |
SELECT * FROM Customer WHERE State = 'CA'
| 3175 | Sam's Small Appliances | 100 Main St. | Anytown | CA | 98765 |
= <> < <= > >= LIKE
SELECT * FROM Customer WHERE State <> 'CA'
| _ | Any single character |
| % | Any sequence of characters |
Name LIKE '_o%'matches "Toaster", but not "Crowbar"
AND OR NOT
SELECT *
FROM Product
WHERE Unit_Price < 100
AND Description <> 'Toaster'
SELECT Name, City FROM Customer WHERE State = 'CA'
COUNT SUM AVG MAX MIN
SELECT COUNT(*) FROM Customer WHERE State = 'CA'
SELECT AVG(Unit_Price) FROM Product
SELECT Product_Code FROM Product WHERE Description = 'Car vacuum'
SELECT Invoice_Number FROM Item WHERE Product_Code = '643-119'
TableName.ColumnName
SELECT Item.Invoice_Number
FROM Product, Item
WHERE Product.Description = 'Car vacuum'
AND Product.Product_Code = Item.Product_Code
| 11731 |
| 11733 |
SELECT Customer.City, Customer.State, Customer.Zip
FROM Product, Item, Invoice, Customer
WHERE Product.Description = 'Hair dryer'
AND Product.Product_Code = Item.Product_Code
AND Item.Invoice_Number = Invoice.Invoice_Number
AND Invoice.Customer_Number
= Customer.Customer_Number
| Anytown | CA |
DELETE FROM Customer WHERE State = 'CA'
UPDATE Item SET Quantity = Quantity + 1 WHERE Invoice_Number = '11731'
General sequence to install and test installation:
To connect from C++ (specific to MySQL):
MYSQL* connection = mysql_init();
mysql_real_connect(connection, NULL, NULL, NULL,
"bigcpp", 0, NULL, 0)
Arguments are:
if (mysql_real_connect(...) == NULL)
{
string error_message = mysql_error(connection);
. . .
}
mysql_close(connection);
string new_value = ...;
string query = "INSERT INTO Test VALUES ('" + new_value + "')";
mysql_query(query.c_str());
int qty = ...;
string command = "SELECT * FROM Item WHERE Quantity >= "
+ int_to_string(qty);
mysql_query(connection, command.c_str());
if (mysql_query(connection, command.c_str()) != 0)
{
cout << "Error: " << mysql_error(connection) << "\n";
...
}
MYSQL_RES* result = mysql_store_result(connection);
int rows = mysql_num_rows(result);
int fields = mysql_num_fields(result);
for (int r = 1; r <= rows; r++)
{
MYSQL_ROW row = mysql_fetch_row(result);
// inspect field data from the current row
}
string field = row[i];
SELECT Unit_Price, ... FROM Product WHERE ...
To retrieve a record's price:
double price = string_to_double(row[0]);
mysql_free_result(result);
execsql < product.sql
execsql
printinv.cpp
string query = "SELECT Customer_Number, Payment FROM Invoice "
"WHERE Invoice_Number = '" + invnum + "'";
(C++ concatenates adj. string literals)
MYSQL_ROW row = mysql_fetch_row(result);
string custnum = row[0];
double payment = string_to_double(row[1]);
printinv.cpp
string query = "SELECT Item.Quantity, Product.Description, "
"Product.Unit_Price FROM Item, Product WHERE "
"Item.Invoice_Number = ' "+ invnum
+ "'AND Item.Product_Code = Product.Product_Code";
string query = "SELECT SUM(Item.Quantity * Product.Unit_Price) "
"FROM Item, Product WHERE Item.Invoice_Number = '"
+ invnum + "' AND Item.Product_Code = Product.Product_Code";
Yields a single row w/a single field
MYSQL_ROW row = mysql_fetch_row(result); double amount_due = string_to_double(row[0]);
addinv.cpp
SELECT MAX(Invoice_Number) FROM Invoice
string command = "INSERT INTO Item VALUES ('" + invnum +
"', '" + prodcode + "', " + int_to_string(quantity) + ")";
Transactions