Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Sunday, December 25, 2011

How to delete columns from an existing table in SQLite.

SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;

Tuesday, November 1, 2011

how to: python mongodb map-reduce

it's simple example how-to use map-reduce in mongo with python script
use:

  • python3
  • pymongo3 library





import pymongo
from datetime import datetime, timedelta
from  pymongo.code import Code
MAP_FUNCTION = Code("function () { emit(this.player, { count:1, score:this.score} ); }")
REDUCE_FUNCTION = Code("function (key, values) {var result = {count:0, score:0}; values.forEach(function (value) {result.count += value.count; result.score += value.score;}); return result; }")
dbhost=""
dbport=27017
mongodb = pymongo.Connection(dbhost, dbport)
db = mongodb.
age=10 
query={'last_visited': {'$gte': datetime.utcnow()-timedelta(age)}}
result = db..map_reduce(map=MAP_FUNCTION, reduce=REDUCE_FUNCTION, out="output", query=query)
for item in result.find():
    player = item["_id"]
    score = item["value"]['score']
    print(player, score)