Site Sponsors:
An Easy PHP CRUD Example ... for SQL / Sqlite! 
When our needs are simple, then so should be our code.

For 100% of what we need to do at the moment (manage a massive collection of data across a local network,) this class will do what we need it to do: CRUD far too many rows consisting of TEXT / string values!


So, is DAO-generation necessary? When using a name-associated array in PHP 5, all we need do is to define our fields, and away we go!


class SqlStringRecord {

function get_file_name() {
return "default.sqlt3";
}

function get_table_name() {
return "contacts";
}

function get_columns() {
$data = array();
$data[0] = "Name";
$data[1] = "Address";
$data[2] = "Note";
return $data;
}

function delete_table() {
$db = new SQLite3($this->get_file_name());
$tbl = $this->get_table_name();
$db->exec("DROP TABLE IF EXISTS $tbl");
$db->close();
if ($db->lastErrorCode() != 0) {
return FALSE;
}
return TRUE;
}

function create_table() {
$this->delete_table();
$fields = $this->get_columns();
$tbl = $this->get_table_name();
$cmd = "CREATE TABLE $tbl (ID INTEGER PRIMARY KEY AUTOINCREMENT";
foreach ($fields as $field) {
$cmd .= ', ';
$cmd .= $field;
$cmd .= ' STRING';
}
$cmd .= ")";
$db = new SQLite3($this->get_file_name());
$db->exec($cmd);
$db->close();
if ($db->lastErrorCode() != 0) {
return FALSE;
}
return TRUE;
}

function count() {
$db = new SQLite3($this->get_file_name());
$tbl = $this->get_table_name();
$rs = $db->query("SELECT COUNT(*) FROM $tbl");
if ($rs == FALSE) {
return FALSE;
}
$data = $rs->fetchArray();
$db->close();
return $data[0];
}

function create($special) {
$cols = $this->get_columns();
$tbl = $this->get_table_name();
$cmd = "INSERT INTO $tbl (";
$bfirst = true;
foreach ($cols as $col) {
if ($bfirst == false) {
$cmd .= ", ";
}
$cmd .= $col;
$bfirst = false;
}
$cmd .= ") VALUES (";
$bfirst = true;
foreach ($cols as $col) {
if ($bfirst == false) {
$cmd .= ", ";
}
$cmd .= "'$special[$col]'";
$bfirst = false;
} $cmd .= ")";
echo "\n\n$cmd\n\n";
$db = new SQLite3($this->get_file_name());
$db->exec($cmd);
$db->close();
if ($db->lastErrorCode() != 0) {
return FALSE;
}
return $this->count();
}

function read($id) {
$db = new SQLite3($this->get_file_name());
$tbl = $this->get_table_name();
$cmd = "SELECT * FROM $tbl WHERE ID = $id LIMIT 1";
$rs = $db->query($cmd);
if ($rs == FALSE) {
return FALSE;
}
$results = $rs->fetchArray(SQLITE3_ASSOC);
$db->close();
return $results;
}

function update($id, $special) {
$tbl = $this->get_table_name();
$cmd = "UPDATE $tbl SET";
$cols = $this->get_columns();
$bfirst = TRUE;
foreach ($cols as $col) {
if ($bfirst == FALSE) {
$cmd .= ", ";
}
$cmd .= " $col = '$special[$col]'";
$bfirst = FALSE;
}
$cmd .= " WHERE ID = $id";
echo "\n\n$cmd\n\n";
$db = new SQLite3($this->get_file_name());
$db->exec($cmd);
$db->close();
if ($db->lastErrorCode() != 0) {
return FALSE;
}
return TRUE;
}

function delete($id) {
$tbl = $this->get_table_name();
$cmd = "DELETE FROM $tbl WHERE ID = $id";
$db = new SQLite3($this->get_file_name());
$db->exec($cmd);
$db->close();
if ($db->lastErrorCode() != 0) {
return FALSE;
}
return TRUE;
}

}


So feel free to update:

get_columns()
get_table_name() [optional]
get_file_name() [optional]

Then simply use associative arrays as your "data access object" - as returned from the read($id) operation - then accessed via your own get_columns() names - to see how easily (ahem) CRUD happens!

The complete code + test case is here.

[ add comment ] ( 33 views )   |  permalink  |  related link
PyGame Example - The Lazy Star 
Had a chance to teach a bunch of wonderful new Python students this month.

Several were interested in using pygame, so we put together the following demonstration.

Thought we would share it.

Note that - before running pygame - that we will need to use pip (or pip3) to install the pygame package (e.g: pip3 install pygame)

Linux folks should install packages using PIP as to get those Super Cow Powers (i.e either su, or sudo should moo nicely.)

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Hackin' Around!")

back_color = (128, 45, 100) # Ubuntu Default

clock = pygame.time.Clock()

def stat(zEvent):
screen.fill(back_color)
if zEvent is None:
return
try:
loc = zEvent.__dict__['pos']
basicfont = pygame.font.SysFont(None, 72)
text = basicfont.render('*', True, (255, 0, 0), back_color)
screen.blit(text, loc)
except Exception as ex:
print("Exceptional:", zEvent)


stat(None)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: # closing the window
break
stat(event)
pygame.display.update()
clock.tick(12) # fps
pygame.quit()

Note that 'fps' in the above stands for "frames per second." The lower, the slower. -You can set it to 1000 or so to speed things up ... allot!

Sharing is caring!

-Rn

p.s. If you want to learn more about programing in Python 3, then here is a discount coupon to my on-line training.

[ add comment ] ( 25 views )   |  permalink  |  related link
Microsoft Triumph! 
Some people say that we POSIX guys are biased. --Many believe that wee open source folks have something against commercial software platforms / tools.

In my case - as well as everyone else I know - nothing could be farther from the truth!

In very fact, while I personally do allot on free & open Linux / LAMP / POSIX worlds, when it comes to raking in the big bucks I - more often than not - find myself working with Microsoft technologies.

Indeed, we'll rake in ever more $$,$$$ when we combine POSIX, Microsoft, and other tools together (The higher the 'tech is stacked, the fewer there be to tackle it, my friends ...!)

Modern C/C++


While many marvelous tools have come and gone, at the time of this writing - and in my opinion - there are virtually NO decent open graphical tool sets for C/C++. --And I have tried just about all of them! (Conclusion: Never trust a developer - favoring ONE language - to concoct a language-savvy tooling ... for another?)

So as I turned my weary eyes back to Visual Studio this day, I was overjoyed to discover that "The Empire" has finally started to support those of us who - uh - like to live ... on "Hoth" !


(Hey 'Darth: Say "CHEEESE?")

Indeed, after downloading Visual Studio's 2017 Community Edition so that I could play with libtcod (they no longer liked my '2013 Pro) I tell 'ya truly - that a relatively new editor - known as Visual Studio Code - works REAL WELL out of the box... even in our icy monster-caves:



Sure - when time for working on C# & VB comes again (as it always will!) I will surely upgrade the '2013 to VS 2017 -- or whatever else the emperor de-jure requires us to buy so as to continue to move our hostage-ware VS Projects (like libtcod!) forward in their empire.


In the mean time however, it is VERRRRY refreshing to be able to quickly do a 'git - edit - as well as work with the console tools directly from Visual Studio Code, Express, or the Community Editions!

Yet THAT - as we might say - and to complete the analogy - is merely the tip of this iceberg of what the entire VS Family-Empire can do for Modern C/C++! =)

(And yes Slytherin, the 'Empire now even supports Python!)


[ add comment ] ( 34 views )   |  permalink  |  related link

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next> Last>>