Kirix Support Forums

Connecting to db via script

Please post any help questions, requests or other feedback here.

Re: Connecting to db via script

Postby abenitez77 on Thu Feb 03, 2011 4:22 pm

When I comment out the line "db3->execute(..." the error goes away and it works fine.

here is the entire function, I left in all the commented stuff that i will be removing...so it's a bit messy :

//function onConnectToServer(sender, event_args, textbox)
function onButton2Clicked()
{
//Connection
var db2
var db3
var x
var selected_items
// Get the Selected Items into an Array
selected_items = listbox2.getSelectedItems();
// Clear the listbox we will be populating in our for loop below.
listbox.clear();
for (x in selected_items)
{
db2 = new DbConnection("mssql://" + textbox.getText() + ":1434/" + selected_items[x]);
// Adding the connection to the project.
db3->execute("CREATE MOUNT " + selected_items[x] + " AS mssql://" + textbox.getText() + ":1434/" + textbox.getText() );
//alert(selected_items[x] + " is the db");
// Single Connection Code
// db2 = new DbConnection("mssql://" + textbox.getText() + ":1434/" + listbox2.getSelectedItem());

// if (db2.isConnected()) alert("Connection succeeded");
// else alert("Connection failed");

var objects = db2.getObjects();
//listbox.clear();

// Add the Dabase Name first then the related tables below.
//var dbname = selected_items[x] + ":" ;
//listbox.addItem(String.toUpperCase(selected_items[x])+":");
listbox.addItem(selected_items[x].toUpperCase() + ":");
//alert(string.toUpperCase(selected_items[x])+":");
//alert(dbname);

// Loop thru the table names to add them to the listbox
for (var i = 0; i < objects.length; ++i)
{
if (objects[i].type != "TABLE")
continue;

//Add the table names to the listbox
listbox.addItem(objects[i].name);
alert(objects[i].name);
// construct a SQL statement to copy the input to the output
// var sql = "SELECT * INTO Alex" + String.toString(i) + " FROM " + objects[i].name ;

// execute the SQL statement
// var iter = db2.execute(sql);
}

// refresh the project
// HostApp.refresh();
}

}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Thu Feb 03, 2011 5:42 pm

Ok, if you've narrowed it down to that one line, why not narrow it down further? Try building a statement that works using parts of that line, and you should be able to identify the part of the line that the compiler doesn't understand. Then take a closer look at the warning to see what to do to fix it.
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Thu Feb 03, 2011 7:02 pm

I tried changing that line to and still same error. is that the correct syntax? I hardcoded everything with the syntax you gave me and same error. Not sure what to change..

db3->execute("CREATE MOUNT my_mount_name AS mssql://USATL02PRSQ70:1434/STRATA");
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Feb 04, 2011 11:38 am

abenitez77 wrote:I tried changing that line to and still same error. is that the correct syntax? I hardcoded everything with the syntax you gave me and same error. Not sure what to change..

db3->execute("CREATE MOUNT my_mount_name AS mssql://USATL02PRSQ70:1434/STRATA");


I tried db3.execute.... and several other things like changing the "AS" to "ON" ...nothing works. When i changed the db3-> to db3.execute.... it did not error out, but it did not create it in the project either.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Mon Feb 07, 2011 10:03 am

Any ideas?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Mon Feb 07, 2011 10:38 am

Yes. Your problem is that you are using a -> to reference a member function rather than a ".". ECMAScript doesn't use the -> for referencing members, but rather uses "." If you look at your script, you'll see plenty of other examples where you are correctly using the "." to reference the execute() member function on a DbConnection object.

Please try to familiarize yourself with the scripting syntax by taking a look at the examples shown earlier in this thread as well as the tutorials in the links. Once you are more familiar with the syntax, careful comparison of code that works with code that doesn't will help you find your problems and fix them.
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Mon Feb 07, 2011 10:53 am

I did try that as well and it does not give me an error, but does not create the mount either.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Mon Feb 07, 2011 10:54 am

abenitez77 wrote:I did try that as well and it does not give me an error, but does not create the mount either.


db2 = new DbConnection("mssql://USATL02PRSQ70:1434/STRATA");
db2.execute("CREATE MOUNT my_mount AS mssql://USATL02PRSQ70:1434/STRATA");
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Mon Feb 07, 2011 11:03 am

one of the first things I did try was changing the -> to a ".", but since Ben gave me this syntax using the "->", I thought that was the correct one for what we were trying to accomplish and yes, I did look thru the site for syntax examples and could not find one with the pointer "->", but thought it might have been an undocumented one since I could not find a Create mount example anywhere.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Mon Feb 07, 2011 11:14 am

I think I've been programming too much C++ lately :-) Definitely use a "." and not a "->"

Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby Ben on Mon Feb 07, 2011 11:15 am

There reason the mount is not being created is because you are sending the CREATE MOUNT statement to the SQL server, where it needs to be added to your local project.


Code: Select all
HostApp.getDatabase().execute("CREATE MOUNT my_mount AS mssql://USATL02PRSQ70:1434/STRATA");
HostApp.refresh();


Try the above.

Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby abenitez77 on Mon Feb 07, 2011 11:37 am

Ben,
worked like a charm...you guys have been a great help.

I think this may be the last thing. On one of the items in my list, I am populating it with the table names but I am first adding the db name with a semicolon at the end ie "STRATA:". I want to loop thru the list, and highlight the name that has semicolons at the end of the name. The user will be selecting multiple items on my listbox and I want to check whether their selection has a semicolon at the end, so i can remove that from the selection or ignore it. I was thinking of using a substring to check for it...but not sure of the syntax in a listbox...can you help me with the syntax on highlighting and substring?

Thanks
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Tue Feb 08, 2011 7:24 am

Sure. That's an easy one. To check if the last char is a semicolon would look something like this:

Code: Select all
// s is my string with a semicolon
if (s.length > 0 && s[s.length - 1] == ';')
     string has a semicolon


So, I would combine the above with a loop

Code: Select all
var i,s;
for (i = 0; i < listbox.getItemCount(); ++i)
{
    s = listbox.getItem(i);
    if (s.length > 0 && s[s.length - 1] == ';')
         string has a semicolon
}



Best,
Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby abenitez77 on Tue Feb 08, 2011 1:47 pm

This seems to be looping thru the entire list...what if I only want to loop thru the selected items? do i just use Selected() ?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Tue Feb 08, 2011 5:34 pm

I am trying something a little different that looks like will work better with what I already have in my code. I am using the indexOf() function to check for the colon in the selected_items text. I am having an error popup complaining about the semicolon at the end of the if statement. I tried variations of semicolon at the end of the if line...and without ...and with colon at end of if and without semicolon at end of the next line..still same error....can you help me figure this out? Error says : "Compiler error (line 243): Missing semicolon ';'. Line 243 is the line right before the If statement.

for (x in selected_items)
{
if (selected_items[x].indexOf(":") <> -1)
alert("no colon");

db3 = new DbConnection("mssql://" + textbox.getText() + ":1434/" + selected_items2[i]);
alert("mssql://" + textbox.getText() + ":1434/" + selected_items2[i]);

// Test for connected.
// if (db3.isConnected()) alert("Connection succeeded");
// else alert("Connection failed");

var results = db3.execute("Select * Into dbo." + textbox3.getText()+ String.toString(x) + " From " + selected_items[x] + " Where Vnd_Nbr = 'CR-007143'");
else alert("colon");
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Tue Feb 08, 2011 6:29 pm

In your "if" statement, you're using "<>" as a comparison operator, which isn't supported in ECMAScript. Use "!=" in place of "<>". For a list of ECMAScript comparison operators, see here: http://www.w3schools.com/js/js_comparisons.asp
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm
Previous

Return to Strata Help & Feedback