Home mm2k6 Releases New Script Features Database Commands for Text Files
21 | 04 | 2018
Database Commands for Text Files

According to the  poll the second most people preferred using text files for database access (first choice is MySql). This is possible in mm2k6 but is rather limited because text files are not indexed. This means you can add records to them and select records for reading from them but cannot delete or update records. Here's what you need to use database commands on text files.


To access text files as database files you need a file called Schema.ini in the folder you'll have your text db. In it
you need a section for each text file you'll want to use. The section looks like this:

ColNameHeader = False
Format = CSVDelimited
CharacterSet = ANSI
Col1=ClanNumber short
Col2=ClanName char width 50
Col3=ClanLeader char width 20

Results of select statements run against databases go into arrays. For example:

If you have a text file ClanDB.txt with:

1,"Circle of Power","Romsek"
2,"The White Rose","Gnoden"
5,"Legion of Legends","Wonac"


/sqlquery {select * from ClanDB.txt where clannumber > 0}{clans}{txt:@sessionpath()}

creates an array called clans with 4 rows of 3 columns and another array called clansFields with 3 rows with the column names.

/loop {1,4,n}_
{/showme {@a(clans,$n,1) @a(clans,$n,2) @a(clans,$n,3)}}

You can create an empty file or delete the contents of an existing one with

/textout {@sesionpath()ClanDB.txt}{ }

An example of an action to fill a text file using db commands would be:

/action {[%0][ %1] %2}_
/sqlexecute {insert into ClanDB.txt (ClanNumber, ClanName, ClanLeader)
values ($0,'@Ltrim($2)','@Ltrim($1)')}_

Of course if you were to do things like this you'd need to manage the action with disablegroup and enable group and clear the array with /unarray clans if you planned on using it repeatedly.

So an alias to fill a clans database might look something like:

/alias {getclannames}_
  /unarray clans;_
  /textout {@sessionpath()ClanDB.txt}{ };_
  /enablegroup clandb;_
  page -none showclan n;_
  /disablegroup clandb;_