Table creation/Postal addresses: Difference between revisions

→‎{{header|Wren}}: Added a second version using Wren-table.
m (→‎{{header|Phix}}: syntax coloured, marked p2js incompatible)
(→‎{{header|Wren}}: Added a second version using Wren-table.)
 
(7 intermediate revisions by 4 users not shown)
Line 15:
 
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - needed formatted transput}} -->
<langsyntaxhighlight lang="algol68">MODE ADDRESS = STRUCT(
INT page,
FLEX[50]CHAR street,
Line 60:
# set(address table, page OF john brown,1,1); - standard set page not available in a68g #
put bin(address table, john brown);
close(address table)</langsyntaxhighlight>
Output:
<pre>
Line 71:
 
=={{header|Apache Derby}}==
<langsyntaxhighlight SQLlang="sql">create table Address (
addrID integer primary key generated by default as identity,
addrStreet varchar(50) not null,
Line 78:
addrZip char(10) not null
);
</syntaxhighlight>
</lang>
'''Interactive session:
<pre style="height: 30em; overflow: scroll">
Line 129:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">db: open.sqlite "addresses.db"
 
query db {!sql
Line 141:
}
 
close db</langsyntaxhighlight>
 
=={{header|AWK}}==
Line 149:
This version uses the AWK pipe, 'getline' function, and the sqlite3 command line program.
 
<langsyntaxhighlight lang="awk">#!/bin/sh -f
awk '
BEGIN {
Line 181:
}
 
'</langsyntaxhighlight>
 
=={{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}}==
{{libheader|SQLite}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
Line 216 ⟶ 236:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="clojure">(require '[clojure.java.jdbc :as sql])
; Using h2database for this simple example.
(def db {:classname "org.h2.Driver"
Line 232 ⟶ 326:
[:state "varchar"]
[:zip "varchar"]))
</syntaxhighlight>
</lang>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'struct)
(lib 'sql)
Line 252 ⟶ 346:
[0] 15 Gallubert 29 rue de l'Ermitage Paris Seine 75020
[1] 16 Brougnard 666 rue des Cascades Paris Seine 75042
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
Erlang has built in databases. This is the the one with most features: Mnesia. There are database connectors to other databases, too.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( table_creation ).
 
Line 266 ⟶ 360:
mnesia:start(),
mnesia:create_table( address, [{attributes, record_info(fields, address)}] ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 275 ⟶ 369:
=={{header|FunL}}==
FunL has built-in support for H2 and comes bundled with the H2 database engine.
<langsyntaxhighlight lang="funl">import db.*
import util.*
 
Line 311 ⟶ 405:
print( TextTable.apply(result) )
 
conn.close()</langsyntaxhighlight>
 
{{out}}
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}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 371 ⟶ 499:
fmt.Println(field, storage)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 386 ⟶ 514:
{{libheader|sqlite-simple}}
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
import Database.SQLite.Simple
Line 400 ⟶ 528:
\addrZIP TEXT NOT NULL \
\)"
close db</langsyntaxhighlight>
 
=={{header|J}}==
J is a programming language, not a database, but it ships with a database built in the programming language called [[j:JDB|JDB]]. Using that, assuming <tt>hd</tt> is your database, then:
 
<langsyntaxhighlight lang="j"> Create__hd 'Address';noun define
addrID autoid;
addrStreet varchar
Line 411 ⟶ 539:
addrState char
addrZip char
)</langsyntaxhighlight>
 
Of course J can connect external databases too, using e.g. [[j:Studio/ODBC%20Basics|ODBC]]. See the [[j:DB|list of J database topics]].
 
=={{header|Java}}==
Java can connect with many databases.
This example completes the task with a random access file, instead of using an external library.
<syntaxhighlight lang="java">
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
 
public final class TableCreationPostalAddresses {
 
public static void main(String[] args) throws IOException {
Address[] addresses = new Address[] {
new Address("FSF Inc.", "51 Franklin Street", "Boston", "MA", "02110-1301"),
new Address("The White House", "1600 Pennsylvania Avenue NW", "Washington", "DC", "20500"),
new Address("National Security Council", "1700 Pennsylvania Avenue NW", "Washington", "DC", "20500")
};
Path path = Path.of("addresses.dat");
FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.CREATE,
StandardOpenOption.READ,
StandardOpenOption.WRITE);
for ( int i = 0; i < addresses.length; i++ ) {
byte[] data = addresses[i].addressRecord().getBytes();
ByteBuffer writeBuffer = ByteBuffer.wrap(data);
fileChannel.position(i * Address.RECORD_LENGTH);
while ( writeBuffer.hasRemaining() ) {
fileChannel.write(writeBuffer);
}
}
for ( int i = 0; i < addresses.length; i++ ) {
fileChannel.position(i * Address.RECORD_LENGTH);
ByteBuffer readBuffer = ByteBuffer.allocate(Address.RECORD_LENGTH);
fileChannel.read(readBuffer);
System.out.println( new String(readBuffer.array()) );
}
fileChannel.close();
}
}
 
final class Address {
public Address(String aName, String aStreet, String aCity, String aState, String aZipCode) {
name = aName; street = aStreet; city = aCity; state = aState; zipCode = aZipCode;
}
public String addressRecord() {
String record = "";
record += String.format("%-30s", name);
record += String.format("%-30s", street);
record += String.format("%-15s", city);
record += String.format("%-5s", state);
record += String.format("%-10s", zipCode);
return record;
}
public static final int RECORD_LENGTH = 30 + 30 + 15 + 5 + 10;
private String name, street, city, state, zipCode;
}
</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|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using SQLite
 
db = SQLite.DB()
Line 428 ⟶ 629:
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL)
""")</langsyntaxhighlight>
 
=={{header|Kotlin}}==
Rather than use an external database, we use the built-in RandomAccessFile class for his task. The data used is the same as for the REXX entry.
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
import java.io.File
Line 520 ⟶ 721:
println(Address.readRecord(file, i.toLong()))
}
}</langsyntaxhighlight>
 
{{output}}
Line 542 ⟶ 743:
Lasso has excellent support for connecting to and handling databases.
 
<langsyntaxhighlight Lassolang="lasso">// connect to a Mysql database
inline(-database = 'rosettatest', -sql = "CREATE TABLE `address` (
`id` int(11) NOT NULL auto_increment,
Line 553 ⟶ 754:
") => {^
error_msg
^}</langsyntaxhighlight>
Output:
<pre>No error</pre>
Line 559 ⟶ 760:
=={{header|Lua}}==
Using LJSQLite3 - compatible with LuaJIT and supplied in the ULua distribution.
<langsyntaxhighlight Lualang="lua">-- Import module
local sql = require("ljsqlite3")
 
Line 576 ⟶ 777:
 
-- Explicitly close connection
conn:close()</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">TableCreation="CREATE TABLE address (
addrID INTEGER PRIMARY KEY AUTOINCREMENT,
addrStreet TEXT NOT NULL, addrCity TEXT NOT NULL,
Line 585 ⟶ 786:
Needs["DatabaseLink`"]
conn=OpenSQLConnection[ JDBC[ "mysql","databases:1234/conn_test"], "Username" -> "test"]
SQLExecute[ conn, TableCreation]</langsyntaxhighlight>
 
=={{header|MySQL}}==
<langsyntaxhighlight lang="mysql">CREATE TABLE `Address` (
`addrID` int(11) NOT NULL auto_increment,
`addrStreet` varchar(50) NOT NULL default '',
Line 595 ⟶ 796:
`addrZIP` char(10) NOT NULL default '',
PRIMARY KEY (`addrID`)
);</langsyntaxhighlight>
 
=={{header|NetRexx}}==
Line 602 ⟶ 803:
{{libheader|Apache Derby}}
This sample creates a table in an embedded Apache Derby database.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 662 ⟶ 863:
createTable()
return
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import db_sqlite as db
#import db_mysql as db
#import db_postgres as db
Line 682 ⟶ 883:
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL)"""
c.close()</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX ***************************************************************
* 17.05.2013 Walter Pachl translated from REXX version 2
* nice try? improvements are welcome as I am rather unexperienced
Line 725 ⟶ 926:
Say ' state -->' self~state
Say ' zip -->' self~zip
Say copies('-',40)</langsyntaxhighlight>
Output is as for REXX version 2
 
=={{header|Oracle}}==
<langsyntaxhighlight lang="sql">CREATE SEQUENCE seq_address_pk START BY 100 INCREMENT BY 1
/
CREATE TABLE address (
Line 739 ⟶ 940:
CONSTRAINT address_pk1 PRIMARY KEY ( addrID )
)
/</langsyntaxhighlight>
 
=={{header|Oz}}==
Line 748 ⟶ 949:
 
The SQLite version that comes with Ozsqlite does not understand "AUTOINCREMENT".
<langsyntaxhighlight lang="oz">declare
[Sqlite] = {Module.link ['x-ozlib:/sqlite/Sqlite.ozf']}
 
Line 769 ⟶ 970:
finally
{Sqlite.close DB}
end</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use DBI;
 
my $db = DBI->connect('DBI:mysql:database:server','login','password');
Line 788 ⟶ 989:
 
my $exec = $db->prepare($statment);
$exec->execute;</langsyntaxhighlight>
 
This example uses mysql, but DBI supports a extensive list of database drivers. See [http://dbi.perl.org/ dbi.perl.org] for more info.
Line 794 ⟶ 995:
=={{header|Phix}}==
{{libheader|SQLite}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pSQLite</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 813 ⟶ 1,014:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sqlite3_exec error: %d [%s]\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqlite_last_exec_err</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}+SQLite==
{{trans|Python}}
not tested
<langsyntaxhighlight lang="php"><?php
$db = new SQLite3(':memory:');
$db->exec("
Line 829 ⟶ 1,030:
)
");
?></langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Line 837 ⟶ 1,038:
 
Define an "address" entity, and create the database:
<langsyntaxhighlight PicoLisplang="picolisp">(class +Adr +Entity)
(rel nm (+Sn +Idx +String)) # Name [Soundex index]
(rel str (+String)) # Street
Line 848 ⟶ 1,049:
(rel jpg (+Blob)) # Photo
 
(pool "address.db") # Create database</langsyntaxhighlight>
Create a first entry, and show it:
<langsyntaxhighlight PicoLisplang="picolisp">(show
(new! '(+Adr) # Create a record
'nm "FSF Inc."
'str "51 Franklin St"
'st "Boston, MA"
'zip "02110-1301" ) )</langsyntaxhighlight>
Output:
<pre>{2} (+Adr)
Line 863 ⟶ 1,064:
nm "FSF Inc."</pre>
Interactive "select":
<langsyntaxhighlight PicoLisplang="picolisp">(select nm zip +Adr nm "FSF") # Select name, zip from Adr where name = FSF*</langsyntaxhighlight>
Output:
<pre>"FSF Inc." "02110-1301" {2}</pre>
 
=={{header|PostgreSQL}}==
<langsyntaxhighlight lang="sql">CREATE SEQUENCE address_seq start 100;
CREATE TABLE address (
addrID int4 PRIMARY KEY DEFAULT nextval('address_seq'),
Line 875 ⟶ 1,076:
state varchar(2) not null,
zip varchar(20) not null
);</langsyntaxhighlight>
 
=={{header|PowerShell}}+SQLite==
{{libheader|SQLite}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
Import-Module -Name PSSQLite
 
Line 913 ⟶ 1,114:
## View the data
Invoke-SqliteQuery -DataSource $DataSource -Query "SELECT * FROM SSADDRESS" | FormatTable -AutoSize
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 923 ⟶ 1,124:
=={{header|PureBasic}}+SQLite==
Easiest approach with sqlite. Further possible: PostgresQL or each other over ODBC.
<syntaxhighlight lang="purebasic">
<lang Purebasic>
UseSQLiteDatabase()
Procedure CheckDatabaseUpdate(Database, Query$)
Line 946 ⟶ 1,147:
EndIf
closeconsole()
</syntaxhighlight>
</lang>
 
=={{header|Python}}+SQLite==
{{libheader|SQLite}}
<langsyntaxhighlight lang="python">>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> conn.execute('''CREATE TABLE address (
Line 960 ⟶ 1,161:
)''')
<sqlite3.Cursor object at 0x013265C0>
>>> </langsyntaxhighlight>
 
=={{header|Racket}}==
Line 966 ⟶ 1,167:
Racket supports a bunch of DBs, this is using sqlite, which is almost always available. Also included some further demonstrations beyond just the table creation:
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang at-exp racket
 
Line 1,017 ⟶ 1,218:
 
(disconnect postal)
</syntaxhighlight>
</lang>
 
Output:
Line 1,040 ⟶ 1,241:
Like Perl DBI, Raku DBIish supports many different databases. An example using SQLite is shown here.
 
<syntaxhighlight lang="raku" perl6line>use DBIish;
 
my $dbh = DBIish.connect('SQLite', :database<addresses.sqlite3>);
Line 1,053 ⟶ 1,254:
addrZIP TEXT NOT NULL
)
STATEMENT</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,103 ⟶ 1,304:
╚════════════════════════════════════════════════════════════════════════════════════════╝
</pre>
<langsyntaxhighlight lang="rexx">/*REXX program creates, builds, and displays a table of given U.S.A. postal addresses.*/
@usa.=; @usa.0=0; $='@USA.' /*initialize array and first value.*/
@usa.0=@usa.0 + 1 /*bump the unique number for usage.*/
Line 1,136 ⟶ 1,337:
call value $ || @usa.0'.upHist' , userid() date() time()
end
return</langsyntaxhighlight>
{{out|output|text=&nbsp; (data used is within the REXX program):}}
<pre>
Line 1,155 ⟶ 1,356:
 
===version 2===
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 17.05.2013 Walter Pachl
* should work with every REXX.
Line 1,189 ⟶ 1,390:
Say copies('-',40)
End
Return</langsyntaxhighlight>
<pre> name --> FSF Inc.
addr --> 51 Franklin Street
Line 1,205 ⟶ 1,406:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Table creation/Postal addresses
 
Line 1,221 ⟶ 1,422:
 
sqlite_execute(oSQLite,sql)
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Line 1,227 ⟶ 1,428:
PStore implements a persistent key store with transactions. This is a NoSQL database. Each transaction reads the entire database into memory, and then writes it again, so PStore is not good for large databases.
 
<langsyntaxhighlight lang="ruby">require 'pstore'
require 'set'
 
Line 1,236 ⟶ 1,437:
db[:next] ||= 0 # Next available Address#id
db[:ids] ||= Set[] # Set of all ids in db
end</langsyntaxhighlight>
 
To put an Address inside this PStore:
 
<langsyntaxhighlight lang="ruby">db.transaction do
id = (db[:next] += 1)
db[id] = Address.new(id,
Line 1,246 ⟶ 1,447:
"Washington", "DC", 20500)
db[:ids].add id
end</langsyntaxhighlight>
 
===With SQLite===
Line 1,252 ⟶ 1,453:
 
{{libheader|sqlite3-ruby}}
<langsyntaxhighlight lang="ruby">require 'sqlite3'
 
db = SQLite3::Database.new(':memory:')
Line 1,263 ⟶ 1,464:
addrZIP TEXT NOT NULL
)
")</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
AQLite
<langsyntaxhighlight lang="runbasic">sqliteconnect #mem, ":memory:" ' make handle #mem
mem$ = "
CREATE TABLE address (
Line 1,276 ⟶ 1,477:
addrZIP TEXT NOT NULL
)"
#mem execute(mem$)</langsyntaxhighlight>
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sql">
 
PROC SQL;
Line 1,291 ⟶ 1,492:
)
;QUIT;
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
Line 1,299 ⟶ 1,500:
This example works with Chicken Scheme, using its sql-de-lite library:
 
<langsyntaxhighlight lang="scheme">
(use sql-de-lite)
 
Line 1,316 ⟶ 1,517:
 
(close-database *db*) ; finally, close database
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('DBI');
 
var db = %s'DBI'.connect('DBI:mysql:database:server','login','password');
Line 1,336 ⟶ 1,537:
 
var exec = db.prepare(statment);
exec.execute;</langsyntaxhighlight>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
<langsyntaxhighlight lang="sql pl">
CREATE TABLE Address (
addrID Integer generated by default as identity,
Line 1,348 ⟶ 1,549:
addrZIP Char(10) not null
);
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,376 ⟶ 1,577:
=={{header|SQLite}}==
Purely in Sqlite3.
<langsyntaxhighlight lang="sqlite3">
CREATE TABLE address_USA (
address_ID INTEGER PRIMARY KEY,
Line 1,384 ⟶ 1,585:
address_Zip INTEGER
);
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
Line 1,392 ⟶ 1,593:
Other possibilities include using the '''[https://www.stata.com/help.cgi?odbc odbc]''' command or a C or Java plugin to connect to a database. See the FAQ for more details: '''[https://www.stata.com/support/faqs/data-management/using-plugin-to-connect-to-database/ How do I connect to a database by using a Stata plugin?]'''.
 
<langsyntaxhighlight lang="stata">clear
gen str8 addrid=""
gen str50 street=""
Line 1,398 ⟶ 1,599:
gen str2 state=""
gen str20 zip=""
save address</langsyntaxhighlight>
 
=={{header|Tcl}}+SQLite==
{{libheader|SQLite}}
<langsyntaxhighlight lang="tcl">package require sqlite3
 
sqlite3 db address.db
Line 1,413 ⟶ 1,614:
addrZIP TEXT NOT NULL
)
}</langsyntaxhighlight>
 
=={{header|Transact-SQL}} (MSSQL)==
<langsyntaxhighlight lang="sql">CREATE TABLE #Address (
addrID int NOT NULL Identity(1,1) PRIMARY KEY,
addrStreet varchar(50) NOT NULL ,
Line 1,423 ⟶ 1,624:
addrZIP char(10) NOT NULL
)
drop table #Address</langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 1,441 ⟶ 1,642:
.Close
End With
</syntaxhighlight>
</lang>
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">
CLOSE DATABASES ALL
CREATE DATABASE usdata.dbc
Line 1,456 ⟶ 1,657:
OPEN DATABASE usdata.dbc
USE address.dbf SHARED
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight ecmascriptlang="wren">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,568 ⟶ 1,770:
table.removeRecord(1)
System.print("\nThe record with an id of 1 will be deleted, leaving:\n")
table.showRecords()</langsyntaxhighlight>
 
{{out}}
Line 1,602 ⟶ 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>
 
Line 1,607 ⟶ 1,888:
{{trans|AWK}}
Interact with SQLite via the command line.
<langsyntaxhighlight lang="zkl">const NM="address.db";
dbExec(NM,"create table address (street, city, state, zip);");</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn dbExec(db,qry){ dbErrorCheck(dbMakeQuery(db,qry),String(db," : ",qry)) }
fcn dbMakeQuery(db,qry){
qry=dbEscapeQuery(qry) + ";";
Line 1,624 ⟶ 1,905:
throw(Exception.IOError(listOfStrings.concat().strip()));
True
}</langsyntaxhighlight>
9,476

edits