xquery exception [XPTY0019] - exception

I am using the following xml file (users_doc.xml)
<users>
<user trusted="false">
<userid>vsony7#vt.edu</userid>
<password>sony</password>
</user>
<user trusted="false">
<userid>shivi</userid>
<password>shivi</password>
</user>
<user trusted="false">
<userid>xyz</userid>
<password>xyz</password>
</user>
</users>
I am running the following xquery: (Here $doc_name=users_doc, $userid=xyz)
declare variable $doc_name as xs:string external;
declare variable $userid as xs:string external;
let $users_doc := doc($doc_name)/users
return delete node $users_doc/user/userid=$userid/..
I am trying to find a given node <userid>xyz</userid> and if the user exists I would like to delete its parent node
<user trusted="false">
<userid>xyz</userid>
<password>xyz</password>
</user>
But, when I run this query I get the following exception:
Exception in thread "main" java.io.IOException: Stopped at line 5, column 51:
[XPTY0019] Context node required for ..; xs:string found.
How do I fix this ?
Thanks,
Sony

From http://www.w3.org/TR/xquery/#ERRXPTY0019
err:XPTY0019
It is a type error if the result of a step (other than
the last step) in a path expression
contains an atomic value.
Let's look at your expression:
$users_doc/user/userid=$userid/..
The left term of the last step is a node set comparison:
$users_doc/user/userid=$userid
So, it will result in true or false boolean value (an atomic value). Of course, you can't select the parent::node() of true or false...
You want this expression:
$users_doc/user[userid=$userid]

Related

Get the node following a selected node

I have the following (cut down) xml in a sql column
<Values>
<Value>
<N>Test</N>
<V>FindMe</V>
</Value>
<Value>
<N>Test</N>
<V>NotMe</V>
</Value>
<Value>
<N>Other Value</N>
<V>NotMe</V>
</Value>
</Values>
I am trying to get the value of V, following the first N with a value of Test
I have tried using following-sibling, but that apparently is not supported in whatever version of XPATH Sql Server 2008 uses.
I have arrived at the following query, based on Gunther's answer here:
/Values/Value/V[. >> /Values/Value/N[. = 'Test'][1]][1]
Which when tried online, returns multiple rows, but when tried in the following SQL
SELECT
A.x,
A.x.query('/Values/Value/V[. >> /Values/Value/N[. = ''Test''][1]][1]')
FROM (SELECT CAST('<Values><Value><N>Test</N><V>FindMe</V></Value><Value><N>Other Value</N><V>NotMe</V></Value></Values>' AS XML) x) A
Has the following error
XQuery [A.x.query()]: '>>' requires a singleton (or empty sequence), found operand of type 'element(N,xdt:untyped) *'
I've tried the following variations on the query, but all have had the same error
/Values/Value/V[. >> /Values/Value/N[. = 'Test'][1]][1]
/Values/Value/V[. >> /Values/Value[N = 'Test'][1]][1]
/Values/Value/V[. >> /Values/Value[N = 'Test']/N[1]][1]
This should do it.
/Values/Value[N='Test'][1]/V

mysql Invalid column count in CSV input while importing csv file

I have a table in the database called locations which contains 2 columns(id which is auto incremented , location) , And I have a csv contains 1 column looks like :
When I try to import that file to locations table , I get this error invalid column count in CSV input on line 1.
Also I tried CSV using LOAD DATA but I get this : MySQL returned an empty result set (i.e. zero rows)
Maybe you should use:
$array = array_map('str_getcsv', file('file.csv'));
You have more option to check variables.
If Java is supported in your machine, Use below solution
https://dbisweb.wordpress.com/
The simple configuration required to do is
<?xml version="1.0" encoding="UTF-8"?>
<config>
<connections>
<jdbc name="mysql">
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/test1</url>
<user>root</user>
<password></password>
</jdbc>
</connections>
<component-flow>
<execute name="file2table" enabled="true">
<migrate>
<source>
<file delimiter="," header="false" path="D:/test_source.csv"/>
</source>
<destination>
<table connection="mysql">locations</table>
</destination>
<mapping>
<column source="1" destination="location" />
</mapping>
</migrate>
</execute>
</component-flow>
</config>
If you are interested, Same can be achieved by Java code.
Source source = new File("D:/test_source.csv", ',', false);
Destination destination = new Table("locations", new JDBCConnection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test1", "root", ""));
List<Mapping> mapping = new ArrayList<>();
mapping.add(new Mapping("1", "location", null));
Component component = new MigrateExecutor(source, destination, mapping);
component.execute();

Proper way to (get from /insert into) table using Erlang Mysql Driver

I am trying to get erlang-mysql-driver working, I managed to set it up and make queries but there are two things I cannot do.(https://code.google.com/archive/p/erlang-mysql-driver/issues)
(BTW, I am new to Erlang)
So Here is my code to connect MySQL.
<erl>
out(Arg) ->
mysql:start_link(p1, "127.0.0.1", "root", "azzkikr", "MyDB"),
{data, Result} = mysql:fetch(p1, "SELECT * FROM messages").
</erl>
1. I cannot get data from table.
mysql.erl doesn't contain any specific information on how to get table datas but this is the farthest I could go.
{A,B} = mysql:get_result_rows(Result),
B.
And the result was this:
ERROR erlang code threw an uncaught exception:
File: /Users/{username}/Sites/Yaws/index.yaws:1
Class: error
Exception: {badmatch,[[4,0,<<"This is done baby!">>,19238],
[5,0,<<"Success">>,19238],
[6,0,<<"Hello">>,19238]]}
Req: {http_request,'GET',{abs_path,"/"},{1,1}}
Stack: [{m181,out,1,
[{file,"/Users/{username}/.yaws/yaws/default/m181.erl"},
{line,18}]},
{yaws_server,deliver_dyn_part,8,
[{file,"yaws_server.erl"},{line,2818}]},
{yaws_server,aloop,4,[{file,"yaws_server.erl"},{line,1232}]},
{yaws_server,acceptor0,2,[{file,"yaws_server.erl"},{line,1068}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,240}]}]
I understand that somehow I need to get second element and use foreach to get each data but strings are returned in different format like queried string is Success but returned string is <<"Success">>.
{badmatch,[[4,0,<<"This is done baby!">>,19238],
[5,0,<<"Success">>,19238],
[6,0,<<"Hello">>,19238]]}
First Question is: How do I get datas from table?
2. How to insert values into table using variables?
I can insert data into table using this method:
Msg = "Hello World",
mysql:prepare(add_message,<<"INSERT INTO messages (`message`) VALUES (?)">>),
mysql:execute(p1, add_message, [Msg]).
But there are two things I am having trouble,
1. I am inserting data without << and >> operators, because When I do Msg = << ++ "Hello World" >>, erlang throws out an exception (I think I am doing something wrong), i don't know wether they are required but without them I am able to insert data into table except this error bothers me after execution:
yaws code at /Users/{username}/Yaws/index.yaws:1 crashed or ret bad val:{updated,
{mysql_result,
[],
[],
1,
[]}}
Req: {http_request,'GET',{abs_path,"/"},{1,1}}
returned atom is updated while I commanded to insert data.
Question 2 is: How do I insert data into table in a proper way?
Error:
{badmatch,[[4,0,<<"This is done baby!">>,19238],
[5,0,<<"Success">>,19238],
[6,0,<<"Hello">>,19238]]}
Tells you that returned values is:
[[4,0,<<"This is done baby!">>,19238],
[5,0,<<"Success">>,19238],
[6,0,<<"Hello">>,19238]]
Which obviously can't match with either {data, Data} nor {A, B}. You can obtain your data as:
<erl>
out(Arg) ->
mysql:start_link(p1, "127.0.0.1", "root", "azzkikr", "MyDB"),
{ehtml,
[{table, [{border, "1"}],
[{tr, [],
[{td, [],
case Val of
_ when is_binary(Val) -> yaws_api:htmlize(Val);
_ when is_integer(val) -> integer_to_binary(Val)
end}
|| Val <- Row
]}
|| Row <- mysql:fetch(p1, "SELECT * FROM messages")
]}
]
}.
</erl>

Error: bad argument #1 to 'insert' (table expected, got nil)

I am trying to connect to a mysql server using LuaSql via a mysql proxy. I try to execute a simple program (db.lua):
require("luasql.mysql")
local _sqlEnv = assert(luasql.mysql())
local _con = nil
function read_auth(auth)
local host, port = string.match(proxy.backends[1].address, "(.*):(.*)")
_con = assert(_sqlEnv:connect( "db_name", "username", "password", "hostname", "3306"))
end
function disconnect_client()
assert(_con:close())
end
function read_query(packet)
local cur = con:execute("select * from t1")
myTable = {}
row = cur:fetch(myTable, "a")
print(myTable.id,myTable.user)
end
This code executes well when I execute it without mysql-proxy. When I am connecting with mysql-proxy, the error-log displays these errors:
mysql.lua:8: bad argument #1 to 'insert' (table expected, got nil)
db.lua:1: loop or previous error loading module 'luasql.mysql'
mysql.lua is a default file of LuaSql:
---------------------------------------------------------------------
-- MySQL specific tests and configurations.
-- $Id: mysql.lua,v 1.4 2006/01/25 20:28:30 tomas Exp $
---------------------------------------------------------------------
QUERYING_STRING_TYPE_NAME = "binary(65535)"
table.insert (CUR_METHODS, "numrows")
table.insert (EXTENSIONS, numrows)
---------------------------------------------------------------------
-- Build SQL command to create the test table.
---------------------------------------------------------------------
local _define_table = define_table
function define_table (n)
return _define_table(n) .. " TYPE = InnoDB;"
end
---------------------------------------------------------------------
-- MySQL versions 4.0.x do not implement rollback.
---------------------------------------------------------------------
local _rollback = rollback
function rollback ()
if luasql._MYSQLVERSION and string.sub(luasql._MYSQLVERSION, 1, 3) == "4.0" then
io.write("skipping rollback test (mysql version 4.0.x)")
return
else
_rollback ()
end
end
As stated in my previous comment, the error indicates that table.insert (CUR_METHODS, ...) is getting a nil as first arg. Since the first arg is CUR_METHODS, it means that this object CUR_METHODS has not been defined yet. Since this happens near top of the luasql.mysql module, my guess is that the luasql initialization was incomplete, maybe because the mysql DLL was not found. My guess is that the LUA_CPATH does not find the MySQL DLL for luasql, but I'm surprised that you wouldn't get a package error, so something odd is going on. You'll have to dig into the luasql module and C file to figure out why it is not being created.
Update: alternately, update your post to show the output of print("LUA path:", package.path) and print("LUA path:", package.cpath) from your mysql-proxy script and also show the path of folder where luasql is installed and contents of that folder.

Help to interpret a trace to find a sql server deadlock

First of all I must say that I have somewhat basic knowledge of sql server and with that I'm trying to figure out how to resolve a deadlock.
I ran dbcc traceon (1204, -1), executed the culprit code and finally executed the xp_readerrorlog stored proc which gave me the following output:
Deadlock encountered .... Printing deadlock information
Wait-for graph
NULL
Node:1
OBJECT: 9:1093578934:0 CleanCnt:2 Mode:IX Flags: 0x1
Grant List 2:
Grant List 3:
Owner:0x000000008165A780 Mode: IX Flg:0x40 Ref:2 Life:02000000 SPID:57 ECID:0 XactLockInfo: 0x0000000082F00EC0
SPID: 57 ECID: 0 Statement Type: EXECUTE Line #: 1
Input Buf: RPC Event: Proc [Database Id = 9 Object Id = 1877581727]
Requested by:
ResType:LockOwner Stype:'OR'Xdes:0x0000000082E02E80 Mode: S SPID:56 BatchID:0 ECID:0 TaskProxy:(0x00000000826EE538) Value:0x81a6f9c0 Cost:(0/1492)
NULL
Node:2
APPLICATION: 9:0:[Proligent Analytics]:(6ff56412) CleanCnt:2 Mode:X Flags: 0x5
Grant List 2:
Owner:0x000000008165DE40 Mode: X Flg:0x40 Ref:1 Life:00000000 SPID:56 ECID:0 XactLockInfo: 0x0000000082E02EC0
SPID: 56 ECID: 0 Statement Type: OPEN CURSOR Line #: 27
Input Buf: RPC Event: Proc [Database Id = 9 Object Id = 1966630049]
Requested by:
ResType:LockOwner Stype:'OR'Xdes:0x0000000082F00E80 Mode: X SPID:57 BatchID:0 ECID:0 TaskProxy:(0x00000000827B8538) Value:0x83e29d40 Cost:(0/250576)
NULL
Victim Resource Owner:
ResType:LockOwner Stype:'OR'Xdes:0x0000000082E02E80 Mode: S SPID:56 BatchID:0 ECID:0 TaskProxy:(0x00000000826EE538) Value:0x81a6f9c0 Cost:(0/1492)
My problem is that I have no idea how to use this to find out what's going on. I've read that you can get the stored procedure that is getting locked but I don't know how.
Please a few pointers would be appreciated.
Thanks
As #Martin Smith says in his comment: select db_name(9), object_name(1093578934,9), object_name(1966630049,9), object_name(1877581727,9) should give you some of the objects.