Add a method to create map databases

- Add `libminetest.map.MapVessel.create` to initialize an empty map database, and even create the file if it doesn't exist
 - For #12
This commit is contained in:
LeMagnesium 2016-04-11 18:04:36 +02:00
parent 849c12ca18
commit 51c7f1bfa4
2 changed files with 13 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ __pycache__/
*.sqlite
*.mts
env/
libminetest.egg*

View File

@ -442,6 +442,17 @@ class MapVessel:
def __str__(self):
return "mapfile vessel for {0}".format(self.mapfile)
@classmethod
def create(cls, path):
k = cls(path)
try:
k.cur.execute("CREATE TABLE IF NOT EXISTS `blocks` (\n`pos` INT PRIMARY KEY,\n`data` BLOB\n);\n")
except _sql.OperationalError as err:
raise MapError("Couln't create database : {}".format(err))
return k
def get_all_mapblock_ids(self):
try:
self.cur.execute("SELECT \"pos\" from \"blocks\"")
@ -452,7 +463,7 @@ class MapVessel:
def open(self, mapfile, backend = "sqlite3"):
try:
self.conn = _sql.connect(mapfile)
self.conn = _sql.connect(mapfile)#, 10)
self.cur = self.conn.cursor()
except _sql.OperationalError as err:
raise MapError("Error opening database : {0}".format(err))