Classic ASP - How to preserve commas while parsing a CSV file - parsing

The Scenario
4 columns, ID, ParentID, Category, OrderNo and Category can have commas in it e.g "Sales, Manager" or "HR, Recruitment" so I would have to handle that without being able to know that the words have quotes around them as the files they use don't so I would need to handle rows with odd number of commas and then treat those rows as ones with categories with commas inside so it's a little more complicated.

I'd personally use the Microsoft Text Driver to parse CSV files, makes dealing with the data alot easier.
First create a text.dsn file and save it somewhere in your web app (in the example i'll assume its where the CSV file is located)
[ODBC]
DRIVER=Microsoft Text Driver (*.txt; *.csv)
UID=admin
UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=25
MaxBufferSize=512
ImplicitCommitSync=Yes
FIL=text
Extensions=txt,csv,tab,asc
DriverId=27
Then treat it as a normal db connection eg:
strPath = server.mappath("/csv/")
sDSNFile = "text.dsn"
strCSVFile = "test.csv"
sDSN = "FileDSN=" & strPath & sDSNFile & ";DefaultDir=" & strPath & ";DBQ=" & strPath & ";"
Set Conn = CreateObject("ADODB.Connection")
Conn.Open sDSN
sql = "SELECT * FROM [" & strCSVFile & "]"
set rs = conn.execute(sql)
do until rs.eof
id = rs("ID")
ParentID = rs("ParentID")
Category = rs("Category")
OrderNo = rs("orderno")
' do something cool here
loop
This way you could pull say all developers out using standard sql
sql = "SELECT * FROM [" & strCSVFile & "] where Category='Developer'"
Hope this helps.
ps. If you don't have it installed, I think the text driver is included as part of Microsoft Access Database Engine redistributable, but it has been a while so may be wrong :)

Related

JSQL Parser - info on dblink

I m using JSQLPARSER for the first time and I wonder if it s possible to detect remote links (oracle dblink)?
If for example a simple select * from table1#remote
For JSqlParser this would be no special identifier. However, it will accept it as a normal table name. You could extract all used table names using something like:
String sql = "select * from table1#remote";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("table1#remote"));
And after that, you have to check, like I did with junits assertions, if a table name matches remote dblink syntax.

Heroku/postgresql search

I have a database with names of cities that uses all different kinds of characters, from åäö to vietnamese signs. The place names are all in a capitalized format e.g. "New York" and "Aix En Provence".
My problem now is to try to search for these city names in Heroku which uses postgresql.
p = Place.where("upper(name) LIKE '%" + place_name_searched.to_s.upcase.tr('åäöüñï','ÅÄÖÜÑÏ') + "%'").first
This is the best I have come up with but it is only but a fix for åäö etc and doesn't catch all the 100+ different characters there is out there, e.g. "é". The solution is not to fill out that string any further.
A general problem is that upper(name) seems to translate everything fine but upcase can only handle english letters. So the correspoding search will be .where("TÄBY like 'TäBY') if place_name_searched = 'täby'.
So, how can I match postgresql entries such as "Täby" and "Jidd Ḩafş" to corresponding strings in downcase, entered by the user ("täby", "jidd hafs")?
It all works fine in my Mysql-version of the application but once I upload to Heroku it all fails.
Postgres has an extension called "ILIKE" for case insensitive pattern matching.
p = Place.where("upper(name) ILIKE '%" + place_name_searched.to_s.upcase.tr('åäöüñï','ÅÄÖÜÑÏ') + "%'").first
But please note this only works with Postgres, so you need to check which environment you are running on to decide if you would use this one instead of the regular one.

select records having string with single quote -informix

Hey I want to select records with name O'Neil, How can i do that in Informix.
select * from name_table where lastname = 'O'Neil' --> Doesn't Work.
You must escape single quote with another single quote:
select * from name_table where lastname = 'O''Neil';
With Informix, you generally have two choices.
The Standard SQL technique (described by Michal Niklas) always works and is the simple, recommended solution. All appearances of a single quote in a string are doubled up:
SELECT * FROM Name_Table WHERE LastName = 'O'Neill';
An alternative technique that works unless you have set DELIMIDENT in your environment is to use double quotes around the string:
SELECT * FROM Name_Table WHERE LastName = "O'Neill";
If delimited identifiers are enabled by DELIMIDENT, then this has a different meaning; the DBMS will be looking for a column called "O'Neill" in the table (because it is an identifier, not a string).
If you have both quotes in a string, then you have to be be careful:
SELECT * FROM QuoteTable WHERE Quote = 'He said, "Don''t"!';
SELECT * FROM QuoteTable WHERE Quote = "He said, ""Don't""!';
My 2nd answer with proposition of technology change.
I don't know what technology you use, but maybe you can use PreparedStatements. Example in Jython (Python that can use JDBC drivers):
db = DriverManager.getConnection(jdbc_url, usr, passwd)
pstm = db.prepareStatement("select * from name_table where lastname=?")
pstm.setString(1, "O'Neil")
rs = pstm.executeQuery()
while (rs.next()):
print('[%s]' % (rs.getString(1)))
As you see PreparedStatement use ? as a placeholder and then you must fill it using setString() method. It is very useful if you have to do many similar selects, inserts etc. Have a look at Java6 documentation for this: http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html
Similar techniques can be user for ODBC or other technologies.

How to get data out of a recordset in excel?

I have the following code:
Dim cn As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim AppPath As String
Set cn = CreateObject("ADODB.Connection")
AppPath = Application.ActiveWorkbook.Path
Set rs = CreateObject("ADODB.RecordSet")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & AppPath & "\Masterlist_Current_copy.accdb;"
strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"
cn.Open strConnection
Set rs = cn.Execute(strSql)
'Need Code here to get Info out of recordset
I am trying to get information out of the recordset that has the query result being dumped into it. I'm trying to figure out how to query the recordset and get the number of rows with a specific value in the "Neptune Number" field. I will then insert the correct number of rows into the worksheet I'm modifying. After that I need to get the data for that value and insert it into the worksheet.
Note: I don't care if recordset, datatable or anything else is used I simply need to be able to do what I described above. Please show code.
The easiest way to get where you I think you are asking to go is to modify your SQL statement changing
strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components];"
to
strSql = "SELECT [Neptune Number],[Description],[Manufacturer],[Manufacturer P/N] FROM [All Components] WHERE [Neptune Number] = 'Specific Value' ;"
This will force the sql query to only return the records you need. the .find method can be used for filtering the recordset, but i have avoided using it in this instance as it is cleaner to just ask the db for only the information that you want.
to process the recordset you can use the following
with rs
'will skip further processing if no records returned
if not (.bof and .eof) then
'assuming you do not need the headers
'loop through the recordset
do while not .eof
for i = 0 to .fields.count -1
'assuming the active sheet is where you want the data
cells(row, i + colOffset) = .fields(i).value
next
Rows(Row & ":" & Row).Insert
.movenext
loop
end if
end with
Where row is the starting point of your data and colOffset is the starting column of your data. Note that this code does not do things in the exact order you specified in your question (I am inserting rows as needed instead of calcualting the number of records up front.)
I have avoided using .recordcount because I find depending on the database used it will not return a correct record count.

PostgreSql + Query Statement having \r in between the attributes !

Suppose we have a textarea in which we put example string. Textbox contain :
Earth is revolving around the Sun.
But at the time of saving, I just pressed a enter key after "the sun". Now the statements in texbox ::
Earth is revolving around
the Sun
Now, in database where enter was pressed the \r is stored. Now i am trying to fetch the data but unable, because my query is of such type ::
SELECT * FROM "volume_factors" WHERE lower(volume_notes) like E'atest\\r\\n 100'
Actual data stored in database field
atest\r
100
Please suggest me to solve the issue.I have tried gsub to replace but no change.
search_text_array[1] = search_text_array[1].gsub('\\r\\n','\r\n')
Thanks in Advance !!!
Try this:
update volume_factors set volume_notes = regexp_replace(volume_notes, '\r\n', ' ');
That's to replace crlf with one space for data that is already in the database. You use postgresql's psql to do this.
To prevent new data containing crlf entering database, you should do it in the application. If you use ruby's gsub, do not use single quote, use double quote to recognize \n like this:
thestring.gsub("\n", " ")
Here we can replace \r\n by % to fetch the data.
Seaching Query will be like this ::
SELECT * FROM "volume_factors" WHERE lower(volume_notes) like E'atest% 100'
gsub function ::
search_text_array[1] = search_text_array[1].gsub('\\r\\n','%')

Resources