Table creation/Postal addresses: Difference between revisions

→‎{{header|Wren}}: Added a second version using Wren-table.
m (→‎{{header|Wren}}: Minor tidy)
(→‎{{header|Wren}}: Added a second version using Wren-table.)
 
(2 intermediate revisions by one other user 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 397 ⟶ 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,606 ⟶ 1,660:
 
=={{header|Wren}}==
===Version 1===
{{libheader|Wren-dynamic}}
{{libheader|Wren-fmt}}
Line 1,749 ⟶ 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