Table creation/Postal addresses: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured, marked p2js incompatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
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}} -->
<
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)</
Output:
<pre>
Line 71:
=={{header|Apache Derby}}==
<
addrID integer primary key generated by default as identity,
addrStreet varchar(50) not null,
Line 78:
addrZip char(10) not null
);
</syntaxhighlight>
'''Interactive session:
<pre style="height: 30em; overflow: scroll">
Line 129:
=={{header|Arturo}}==
<
query db {!sql
Line 141:
}
close db</
=={{header|AWK}}==
Line 149:
This version uses the AWK pipe, 'getline' function, and the sqlite3 command line program.
<
awk '
BEGIN {
Line 181:
}
'</
=={{header|C}}==
{{libheader|SQLite}}
<
#include <stdlib.h>
#include <sqlite3.h>
Line 216:
}
return EXIT_SUCCESS;
}</
=={{header|Clojure}}==
<
; Using h2database for this simple example.
(def db {:classname "org.h2.Driver"
Line 232:
[:state "varchar"]
[:zip "varchar"]))
</syntaxhighlight>
=={{header|EchoLisp}}==
<
(lib 'struct)
(lib 'sql)
Line 252:
[0] 15 Gallubert 29 rue de l'Ermitage Paris Seine 75020
[1] 16 Brougnard 666 rue des Cascades Paris Seine 75042
</syntaxhighlight>
=={{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">
-module( table_creation ).
Line 266:
mnesia:start(),
mnesia:create_table( address, [{attributes, record_info(fields, address)}] ).
</syntaxhighlight>
{{out}}
<pre>
Line 275:
=={{header|FunL}}==
FunL has built-in support for H2 and comes bundled with the H2 database engine.
<
import util.*
Line 311:
print( TextTable.apply(result) )
conn.close()</
{{out}}
Line 325:
=={{header|Go}}==
<
import (
Line 371:
fmt.Println(field, storage)
}
}</
{{out}}
<pre>
Line 386:
{{libheader|sqlite-simple}}
<
import Database.SQLite.Simple
Line 400:
\addrZIP TEXT NOT NULL \
\)"
close db</
=={{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:
<
addrID autoid;
addrStreet varchar
Line 411:
addrState char
addrZip char
)</
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]].
Line 418:
{{works with|Julia|0.6}}
<
db = SQLite.DB()
Line 428:
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL)
""")</
=={{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.
<
import java.io.File
Line 520:
println(Address.readRecord(file, i.toLong()))
}
}</
{{output}}
Line 542:
Lasso has excellent support for connecting to and handling databases.
<
inline(-database = 'rosettatest', -sql = "CREATE TABLE `address` (
`id` int(11) NOT NULL auto_increment,
Line 553:
") => {^
error_msg
^}</
Output:
<pre>No error</pre>
Line 559:
=={{header|Lua}}==
Using LJSQLite3 - compatible with LuaJIT and supplied in the ULua distribution.
<
local sql = require("ljsqlite3")
Line 576:
-- Explicitly close connection
conn:close()</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
addrID INTEGER PRIMARY KEY AUTOINCREMENT,
addrStreet TEXT NOT NULL, addrCity TEXT NOT NULL,
Line 585:
Needs["DatabaseLink`"]
conn=OpenSQLConnection[ JDBC[ "mysql","databases:1234/conn_test"], "Username" -> "test"]
SQLExecute[ conn, TableCreation]</
=={{header|MySQL}}==
<
`addrID` int(11) NOT NULL auto_increment,
`addrStreet` varchar(50) NOT NULL default '',
Line 595:
`addrZIP` char(10) NOT NULL default '',
PRIMARY KEY (`addrID`)
);</
=={{header|NetRexx}}==
Line 602:
{{libheader|Apache Derby}}
This sample creates a table in an embedded Apache Derby database.
<
options replace format comments java crossref symbols binary
Line 662:
createTable()
return
</syntaxhighlight>
=={{header|Nim}}==
<
#import db_mysql as db
#import db_postgres as db
Line 682:
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL)"""
c.close()</
=={{header|ooRexx}}==
<
* 17.05.2013 Walter Pachl translated from REXX version 2
* nice try? improvements are welcome as I am rather unexperienced
Line 725:
Say ' state -->' self~state
Say ' zip -->' self~zip
Say copies('-',40)</
Output is as for REXX version 2
=={{header|Oracle}}==
<
/
CREATE TABLE address (
Line 739:
CONSTRAINT address_pk1 PRIMARY KEY ( addrID )
)
/</
=={{header|Oz}}==
Line 748:
The SQLite version that comes with Ozsqlite does not understand "AUTOINCREMENT".
<
[Sqlite] = {Module.link ['x-ozlib:/sqlite/Sqlite.ozf']}
Line 769:
finally
{Sqlite.close DB}
end</
=={{header|Perl}}==
<
my $db = DBI->connect('DBI:mysql:database:server','login','password');
Line 788:
my $exec = $db->prepare($statment);
$exec->execute;</
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:
=={{header|Phix}}==
{{libheader|SQLite}}
<!--<
<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:
<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>
<!--</
=={{header|PHP}}+SQLite==
{{trans|Python}}
not tested
<
$db = new SQLite3(':memory:');
$db->exec("
Line 829:
)
");
?></
=={{header|PicoLisp}}==
Line 837:
Define an "address" entity, and create the database:
<
(rel nm (+Sn +Idx +String)) # Name [Soundex index]
(rel str (+String)) # Street
Line 848:
(rel jpg (+Blob)) # Photo
(pool "address.db") # Create database</
Create a first entry, and show it:
<
(new! '(+Adr) # Create a record
'nm "FSF Inc."
'str "51 Franklin St"
'st "Boston, MA"
'zip "02110-1301" ) )</
Output:
<pre>{2} (+Adr)
Line 863:
nm "FSF Inc."</pre>
Interactive "select":
<
Output:
<pre>"FSF Inc." "02110-1301" {2}</pre>
=={{header|PostgreSQL}}==
<
CREATE TABLE address (
addrID int4 PRIMARY KEY DEFAULT nextval('address_seq'),
Line 875:
state varchar(2) not null,
zip varchar(20) not null
);</
=={{header|PowerShell}}+SQLite==
{{libheader|SQLite}}
<syntaxhighlight lang="powershell">
Import-Module -Name PSSQLite
Line 913:
## View the data
Invoke-SqliteQuery -DataSource $DataSource -Query "SELECT * FROM SSADDRESS" | FormatTable -AutoSize
</syntaxhighlight>
{{Out}}
<pre>
Line 923:
=={{header|PureBasic}}+SQLite==
Easiest approach with sqlite. Further possible: PostgresQL or each other over ODBC.
<syntaxhighlight lang="purebasic">
UseSQLiteDatabase()
Procedure CheckDatabaseUpdate(Database, Query$)
Line 946:
EndIf
closeconsole()
</syntaxhighlight>
=={{header|Python}}+SQLite==
{{libheader|SQLite}}
<
>>> conn = sqlite3.connect(':memory:')
>>> conn.execute('''CREATE TABLE address (
Line 960:
)''')
<sqlite3.Cursor object at 0x013265C0>
>>> </
=={{header|Racket}}==
Line 966:
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 at-exp racket
Line 1,017:
(disconnect postal)
</syntaxhighlight>
Output:
Line 1,040:
Like Perl DBI, Raku DBIish supports many different databases. An example using SQLite is shown here.
<syntaxhighlight lang="raku"
my $dbh = DBIish.connect('SQLite', :database<addresses.sqlite3>);
Line 1,053:
addrZIP TEXT NOT NULL
)
STATEMENT</
=={{header|REXX}}==
Line 1,103:
╚════════════════════════════════════════════════════════════════════════════════════════╝
</pre>
<
@usa.=; @usa.0=0; $='@USA.' /*initialize array and first value.*/
@usa.0=@usa.0 + 1 /*bump the unique number for usage.*/
Line 1,136:
call value $ || @usa.0'.upHist' , userid() date() time()
end
return</
{{out|output|text= (data used is within the REXX program):}}
<pre>
Line 1,155:
===version 2===
<
* 17.05.2013 Walter Pachl
* should work with every REXX.
Line 1,189:
Say copies('-',40)
End
Return</
<pre> name --> FSF Inc.
addr --> 51 Franklin Street
Line 1,205:
=={{header|Ring}}==
<
# Project : Table creation/Postal addresses
Line 1,221:
sqlite_execute(oSQLite,sql)
</syntaxhighlight>
=={{header|Ruby}}==
Line 1,227:
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.
<
require 'set'
Line 1,236:
db[:next] ||= 0 # Next available Address#id
db[:ids] ||= Set[] # Set of all ids in db
end</
To put an Address inside this PStore:
<
id = (db[:next] += 1)
db[id] = Address.new(id,
Line 1,246:
"Washington", "DC", 20500)
db[:ids].add id
end</
===With SQLite===
Line 1,252:
{{libheader|sqlite3-ruby}}
<
db = SQLite3::Database.new(':memory:')
Line 1,263:
addrZIP TEXT NOT NULL
)
")</
=={{header|Run BASIC}}==
AQLite
<
mem$ = "
CREATE TABLE address (
Line 1,276:
addrZIP TEXT NOT NULL
)"
#mem execute(mem$)</
=={{header|SAS}}==
<
PROC SQL;
Line 1,291:
)
;QUIT;
</syntaxhighlight>
=={{header|Scheme}}==
Line 1,299:
This example works with Chicken Scheme, using its sql-de-lite library:
<
(use sql-de-lite)
Line 1,316:
(close-database *db*) ; finally, close database
</syntaxhighlight>
=={{header|Sidef}}==
{{trans|Perl}}
<
var db = %s'DBI'.connect('DBI:mysql:database:server','login','password');
Line 1,336:
var exec = db.prepare(statment);
exec.execute;</
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
<
CREATE TABLE Address (
addrID Integer generated by default as identity,
Line 1,348:
addrZIP Char(10) not null
);
</syntaxhighlight>
Output:
<pre>
Line 1,376:
=={{header|SQLite}}==
Purely in Sqlite3.
<
CREATE TABLE address_USA (
address_ID INTEGER PRIMARY KEY,
Line 1,384:
address_Zip INTEGER
);
</syntaxhighlight>
=={{header|Stata}}==
Line 1,392:
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?]'''.
<
gen str8 addrid=""
gen str50 street=""
Line 1,398:
gen str2 state=""
gen str20 zip=""
save address</
=={{header|Tcl}}+SQLite==
{{libheader|SQLite}}
<
sqlite3 db address.db
Line 1,413:
addrZIP TEXT NOT NULL
)
}</
=={{header|Transact-SQL}} (MSSQL)==
<
addrID int NOT NULL Identity(1,1) PRIMARY KEY,
addrStreet varchar(50) NOT NULL ,
Line 1,423:
addrZIP char(10) NOT NULL
)
drop table #Address</
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 1,441:
.Close
End With
</syntaxhighlight>
=={{header|Visual FoxPro}}==
<
CLOSE DATABASES ALL
CREATE DATABASE usdata.dbc
Line 1,456:
OPEN DATABASE usdata.dbc
USE address.dbf SHARED
</syntaxhighlight>
=={{header|Wren}}==
Line 1,463:
{{libheader|Wren-sort}}
We use the same simple database format for this task as we did for the [[Table_creation#Wren]] task.
<
import "/fmt" for Fmt
import "/sort" for Cmp, Sort
Line 1,568:
table.removeRecord(1)
System.print("\nThe record with an id of 1 will be deleted, leaving:\n")
table.showRecords()</
{{out}}
Line 1,607:
{{trans|AWK}}
Interact with SQLite via the command line.
<
dbExec(NM,"create table address (street, city, state, zip);");</
<
fcn dbMakeQuery(db,qry){
qry=dbEscapeQuery(qry) + ";";
Line 1,624:
throw(Exception.IOError(listOfStrings.concat().strip()));
True
}</
|