Table creation/Postal addresses: Difference between revisions

→‎{{header|Wren}}: Added a second version using Wren-table.
m (Minor code improvement.)
(→‎{{header|Wren}}: Added a second version using Wren-table.)
 
(4 intermediate revisions by 3 users not shown)
Line 182:
 
'</syntaxhighlight>
 
=={{header|BASIC256}}==
<syntaxhighlight lang="vbnet"># create a new database file or open it
dbopen "addresses.sqlite3"
 
# delete the existing table - If it is a new database, the error is captured
onerror errortrap
dbexecute "drop table addresses;"
offerror
 
# create the table
dbexecute "CREATE TABLE addresses (addrID integer, addrStreet string, addrCity string, addrState string, addrZIP string);"
 
# close all
dbclose
end
 
errortrap:
# accept the error - show nothing - return to the next statement
return</syntaxhighlight>
 
=={{header|C}}==
Line 217 ⟶ 237:
return EXIT_SUCCESS;
}</syntaxhighlight>
 
=={{header|C++}}==
This example completes the task with a random access file, instead of using an external library.
<syntaxhighlight lang="c++">
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
 
class Address {
public:
Address(const std::string& aName, const std::string& aStreet, const std::string& aCity,
const std::string& aState, const std::string& aZipCode)
: name(aName), street(aStreet), city(aCity), state(aState), zipCode(aZipCode) {}
 
std::string address_record() {
std::string record;
record += fixed_length(name, 30);
record += fixed_length(street, 30);
record += fixed_length(city, 15);
record += fixed_length(state, 5);
record += fixed_length(zipCode, 10);
return record;
}
 
static constexpr uint32_t RECORD_LENGTH = 90;
 
private:
std::string fixed_length(const std::string& text, const uint64_t& size) {
return ( text.length() > size ) ? text.substr(0, size) : text + std::string(size - text.length(), ' ');
}
 
std::string name, street, city, state, zipCode;
};
 
int main() {
std::vector<Address> addresses = {
Address("FSF Inc.", "51 Franklin Street", "Boston", "MA", "02110-1301"),
Address("The White House", "1600 Pennsylvania Avenue NW", "Washington", "DC", "20500"),
Address("National Security Council", "1700 Pennsylvania Avenue NW", "Washington", "DC", "20500")
};
 
std::fstream file("addresses.dat", std::ios::app | std::ios::in | std::ios::out);
if ( ! file ) {
std::cerr << "Error. Cannot open file." << std::endl;
exit(EXIT_FAILURE);
}
 
for ( uint64_t i = 0; i < addresses.size(); ++i ) {
file.seekp(i * Address::RECORD_LENGTH, std::ios::beg);
file << addresses[i].address_record();
}
 
for ( uint64_t i = 0; i < addresses.size(); ++i ) {
file.seekg(i * Address::RECORD_LENGTH, std::ios::beg);
char ch;
std::string address;
while ( address.length() < Address::RECORD_LENGTH ) {
file.get(ch);
address += ch;
}
std::cout << address << std::endl;
}
 
file.close();
}
</syntaxhighlight>
{{ out }}
<pre>
FSF Inc. 51 Franklin Street Boston MA 02110-1301
The White House 1600 Pennsylvania Avenue NW Washington DC 20500
National Security Council 1700 Pennsylvania Avenue NW Washington DC 20500
</pre>
 
=={{header|Clojure}}==
Line 323 ⟶ 417:
+----+-----------------+---------------------------+----------+--------+---------+------------+----------------+
</pre>
 
=={{header|FreeBASIC}}==
{{libheader|SQLite}}
<syntaxhighlight lang="vbnet">#include once "sqlite3.bi"
 
Const NULL As Any Ptr = 0
 
Dim As sqlite3 Ptr db
Dim As zstring Ptr errMsg
 
If sqlite3_open(":memory:", @db) <> SQLITE_OK Then
Print "Could not open database: "; sqlite3_errmsg(db)
sqlite3_close(db)
Sleep
End 1
End If
 
Dim As String sql = "CREATE TABLE address(" _
& "addrID INTEGER PRIMARY KEY AUTOINCREMENT," _
& "addrStreet TEXT NOT NULL," _
& "addrCity TEXT NOT NULL," _
& "addrState TEXT NOT NULL," _
& "addrZIP TEXT NOT NULL);"
 
If sqlite3_exec(db, sql, NULL, NULL, @errMsg) <> SQLITE_OK Then
Print "Error creating table: "; *errMsg
sqlite3_free(errMsg)
Else
Print "Table created successfully"
End If
 
sqlite3_close(db)
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
Line 1,532 ⟶ 1,660:
 
=={{header|Wren}}==
===Version 1===
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
{{libheader|Wren-sort}}
We use the same simple database format for this task as we did for the [[Table_creation#Wren]] task.
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Enum, Tuple
import "./fmt" for Fmt
import "./sort" for Cmp, Sort
 
var FieldType = Enum.create("FieldType", ["text", "num", "int", "bool"])
Line 1,675 ⟶ 1,804:
2 FSF Inc. 51 Franklin Street Boston MA 02110-1301
3 National Security Council 1700 Pennsylvania Avenue NW Washington DC 20500
</pre>
 
===Version 2===
{{libheader|Wren-table}}
The above module provides a more generic way to create simple databases and was not available when the first version was written.
<syntaxhighlight lang="wren">import "./table" for Table, FieldInfo, Records
 
var fields = [
FieldInfo.new("id", Num),
FieldInfo.new("name", String),
FieldInfo.new("street", String),
FieldInfo.new("city", String),
FieldInfo.new("state", String),
FieldInfo.new("zipCode", String)
]
 
// create table
var table = Table.new("Addresses", fields)
 
// add records in unsorted order
table.addAll([
[2, "FSF Inc.", "51 Franklin Street", "Boston", "MA", "02110-1301"],
[1, "The White House", "The Oval Office 1600 Pennsylvania Avenue NW", "Washington", "DC", "20500"],
[3, "National Security Council", "1700 Pennsylvania Avenue NW", "Washington", "DC", "20500"]
])
 
var colWidths = [2, 25, 43, 10, 2, 10] // for listings
 
// show the table's fields
table.listFields()
System.print()
 
// sort the records by 'id' and show them
var sortFn = Fn.new { |s, t| s[0] < t[0] }
var records = table.sortedRecords(sortFn)
Records.list(table.fields, records, "Records for %(table.name) table:\n", colWidths)
 
// find a record by key
System.print("\nThe record with an id of 2 is:")
System.print(table.find(2))
 
// delete a record by key
table.remove(1)
System.print("\nThe record with an id of 1 will be deleted, leaving:\n")
records = table.sortedRecords(sortFn)
Records.list(table.fields, records, "Records for %(table.name) table:\n", colWidths)</syntaxhighlight>
 
{{out}}
<pre>
Fields for Addresses table:
 
name kind
------- ------
id Num
name String
street String
city String
state String
zipCode String
 
Records for Addresses table:
 
id name street city st zipCode
-- ------------------------- ------------------------------------------- ---------- -- ----------
1 The White House The Oval Office 1600 Pennsylvania Avenue NW Washington DC 20500
2 FSF Inc. 51 Franklin Street Boston MA 02110-1301
3 National Security Council 1700 Pennsylvania Avenue NW Washington DC 20500
 
The record with an id of 2 is:
[2, FSF Inc., 51 Franklin Street, Boston, MA, 02110-1301]
 
The record with an id of 1 will be deleted, leaving:
 
Records for Addresses table:
 
id name street city st zipCode
-- ------------------------- ------------------------------------------- ---------- -- ----------
2 FSF Inc. 51 Franklin Street Boston MA 02110-1301
3 National Security Council 1700 Pennsylvania Avenue NW Washington DC 20500
</pre>
 
9,476

edits