[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Jscript
. . -> [Subject]  world.DatabaseLastInsertRowid("dbName") can't get rowid
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

world.DatabaseLastInsertRowid("dbName") can't get rowid

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Spark   (6 posts)  [Biography] bio
Date Fri 28 Sep 2012 01:22 AM (UTC)

Amended on Fri 28 Sep 2012 01:40 PM (UTC) by Spark

Message

CREATE TABLE IF NOT EXISTS  rooms(roomid INTEGER NOT NULL PRIMARY KEY autoincrement,
roomname  TEXT NOT NULL,roomexits TEXT,
roomuniquelevel INTEGER DEFAULT 1, 
roomtype INTEGER DEFAULT 1,
roomdesc TEXT,roomext TEXT)
.......

insert into rooms(roomname,roomexits,roomtype,roomuniquelevel,roomdesc,roomext) 
values('testroom','east|north',1,1,'hello room','test');
......
var ret = world.DatabaseExec("dbName",sql);
		var _retNum = world.DatabaseLastInsertRowid("dbName");


world.note("ret is "+ ret);



window shows : "ret is"
that means after insert is done,and i can view the record in database,but i can't get the roomid's value by call world.DatabaseLastInsertRowid("dbName");
【question】 Am i worry use the function ? or the function has bug in javascript? thanks for help.
【remark】 language:js mushclient version: 4.82
[Go to top] top

Posted by Kyrock   (20 posts)  [Biography] bio
Date Reply #1 on Fri 28 Sep 2012 03:41 AM (UTC)
Message
-Just- read the help files on this stuff,so yeah... if you're not getting 0, then DatabaseExec failed in some way and you should use DataBaseError to get what's going on? Also, you did open the database some where before this code right? And, out of curiosity, is it legal to mix up the order of parameters like you are doing, as well as only give 5 of them in an insert?
[Go to top] top

Posted by Nick Gammon   Australia  (21,323 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Fri 28 Sep 2012 04:31 AM (UTC)
Message
Testing your code in Lua (with which I am more comfortable) I get this code:



DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)

DatabaseExec ("db", [[
    CREATE TABLE IF NOT EXISTS rooms 
   (roomid INTEGER NOT NULL PRIMARY KEY autoincrement,
   roomname TEXT NOT NULL,
   roomexits TEXT,
   roomuniquelevel INTEGER DEFAULT 1, 
   roomtype INTEGER DEFAULT 1,
   roomdesc TEXT,
   roomext TEXT)

]])

print (DatabaseError("db"))
      
sql = [[ insert into rooms(roomname,roomexits,roomtype,roomuniquelevel,roomdesc,roomext) 
          values('testroom','east|north',1,1,'hello room'); ]]


DatabaseExec("db",sql);

print (DatabaseError("db"))

DatabaseClose ("db")  -- close it


Results:


not an error
5 values for 6 columns


So the create table worked OK, but not the insert. You should be checking for a zero return result from DatabaseExec.

As Kyrock said, you only have 5 values for 6 rows.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Spark   (6 posts)  [Biography] bio
Date Reply #3 on Fri 28 Sep 2012 12:57 PM (UTC)

Amended on Fri 28 Sep 2012 01:37 PM (UTC) by Spark

Message

   __sql="insert into rooms(roomname,roomexits,roomtype,roomuniquelevel,roomdesc,roomext) 
values('dd','west|east|north|south',1,2,'hh','')"



doInsert	:	function(__sql){
		var _sql = __sql;
		var _ret = world.DatabaseExec(this.dbName,_sql);
		var _retNum = world.DatabaseLastInsertRowid(this.dbName);
		world.note ("_ret="+_ret+",_retNum="+_retNum+";DatabaseLastInsertRowid = "+ world.DatabaseError(this.dbName));
		
		if(world.DatabaseError(this.dbName)=="not an error"){
			util.todebug ("exec insert SQL:"+__sql);
		}else{
			util.toerror("exec error:"+world.DatabaseError(this.dbName)+";result:"+_ret+";sql="+__sql);
		}
		_ret=null;
		_sql=null;
		return _retNum;
	}



var _db = new DB("mydb");
			_db.openDB();
			_ret=_db.doInsert(_sql);
			_db.closeDB();

and the log is:

_ret=0,_retNum=;DatabaseLastInsertRowid = not an error

so we can see insert sql is well done ,and the return value _ret is 0,and i also can see the record in sqlite,but the rowid _retNum is empty.
[Go to top] top

Posted by Nick Gammon   Australia  (21,323 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Fri 28 Sep 2012 10:22 PM (UTC)
Message
You have found a bug. Fixed in version 4.83.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Spark   (6 posts)  [Biography] bio
Date Reply #5 on Sat 29 Sep 2012 12:40 AM (UTC)
Message
so waiting ver 4.83.
thanks all.
[Go to top] top

Posted by Nick Gammon   Australia  (21,323 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Sat 29 Sep 2012 03:35 AM (UTC)
Message
Version 4.83 now available.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Spark   (6 posts)  [Biography] bio
Date Reply #7 on Sat 29 Sep 2012 07:39 AM (UTC)
Message
thanks a lot .
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


4,747 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]