English 中文(简体)
第1栏:通过Oracle DB连接的表格中的名称
原标题:calling the column_name in the table while connecting via Oracle DB

I am trying to get the value from the table(employee)connecting through the oracle database. Since there are 100s of values in one column, I would need to iterate the table and get the exact value.

I have the code that works if I use the index no. such as row[1] but I wanted to use the column_name "first name" instead of row[1]. Below is the code that I have which works. Code:

def load_borrower

connection = OCI8.new( usrname ,  pwd , //host:portno/sid ) 

connection.exec(("SELECT BI_PREFIX, BI_FNAME, BI_MNAME, BI_LNAME, B.BI_SUFFIX, BI_ID_TYPE, BI_ID_NUMBER, BI_DOB,    B1.*, R.*, M.*, C.*, L.* FROM EMPLOYEE, SC_BORROWERPREF_NEW S1, BORROWER_NEW B, BORROWERPREF_NEW B1, RES_ADD R, MAIL_ADD M, CLOS_ADD C, LLORD_ADD L WHERE S2=SCENARIO_ID = S1.SCENARIO_ID AND S1.PREF_ID = B1.PREF_ID AND B1.BORROWER_ID = B.BORROWER_ID AND B1.PREF_ID = R.RES_PREF_ID AND B1.PREF_ID = M.MAIL_PREF_ID AND B1.PREF_ID = C.CLOS_PREF_ID AND B1.PREF_ID = L.LLORD_PREF_ID  AND S.RELEASE_ID= "1" AND S.SCENARIO_NO =  2  ORDER BY S1.SC_BORROWERPREF_ID") do |row|

$BI_PREFIX=row[0].to_s

$BI_FNAME=row[1].to_s

$BI_MNAME=row[2].to_s

$BI_LNAME=row[3].to_s

$BI_SUFFIX=row[4].to_s

$BI_BI_ID_TYPE=row[5].to_s

$BI_BI_ID_NUMBER=row[6].to_s

$BI_DOB=row[7].to_s

$BI_EMAIL=row[9].to_s

$BI_CELL_PH=row[11].to_s

$BI_WORK_PH=row[12].to_s

$BI_PREF_CONT=row[13].to_s

$BI_MAR_STATUS=row[16].to_s

$BI_EMP_STATUS=row[23].to_s

$BI_EDUC_YEARS=row[17].to_s

$BI_NUM_DEPEND=row[21].to_s

end

end

Now I m running the above functions below

load_borrower

因此,上述法典现在可进行罚款。 但是,正如你从上可以看到的那样,我把 d表中的变数界定为[5]、[24] 等,虽然它运作良好,但这种变数非常犹豫和耗费时间。 因此,我很想知道,我们是否有任何方法或指挥来使用这个栏——名称,这样它就能够从行文和诸如行文等一栏获得价值,而不是找到每个栏目——名称的索引。

我不清楚这是否是鲁比拉的缺点,因为它把 d的表格视为一个阵列,可能是因为我们为什么可以通过一栏——名称来说明。

问题回答

首先,你似乎被你所使用的各种技术的界限和分界线混为一谈。 你提供的法典中没有瓦伊。 它从OCI8 Gem手中夺走了全部纯鲁比和一条细微的 st子。 “GEM”是一种标准方式,可借以向Rubru民间人分发以鲁比语书写的密码图书馆和方案。 See HERE, for more info to better knowledge what a Gem is and how they are used.

Watir is another Ruby gem that is for driving web-browsers, and you might be using it elsewhere in your code, but it doesn t relate to this question or OCI8 other than both of them being Ruby code libraries distributed as Gems. So lets leave it aside so as to not confuse things.

The behavior you are seeing is how the OCI8 gem works, NOT anything to do with Ruby specifically. If you want something more elegant, then look into different gems that have been created for doing db access with Ruby, for example ActiveRecord, which was suggested in another answer already. The OCI8 Gem only returns an array if you have the results feeding into a block like you do in your current code. Otherwise the results are in an object called a Cursor, and you can use the cursor s fetch_hash method to get fetched data as a Hash. The hash keys are column names. (see http://ruby-oci8.rubyforge.org/en/api_OCI8Cursor.html)

请允许我强烈建议,在你把比拉语进一步推向目前项目之前,你花了多少时间学习比鲁语。 鉴于你似乎正在做的编码性质,我建议你阅读布赖恩·马里克的书《同鲁比书》一书,这本书将使你更好地了解你正在使用的技术,而且正如我刚才所做的那样,你会更好地了解一下。

如果你允许就你如何与你的数据库相互连接提供一些一般性建议。 IMHO, 你们应当利用b子,制造一种疑问,即把你想要的数据归来,而不是gra取大量数据,并试图通过人工授精。 它更好地利用资源,利用较少的记忆,花较少时间从 d子中转移胎儿,不管贵方的教法如何好,它还是像Oracle人民所写的那样赢得了好。 让我们去做重提,这是它给它带来的东西。

如果你在此处理的数据是推动你的测试或验证结果,而不是建造一个庞大的单体阵列,那么我建议你采用一种更加组合的方法。 使用一种全球变量,如EMP_ 现有用户的识别器正在与您进行测试或对照,而且测试代码对每个验证所需的价值,或像地址部分这样的一个小的逻辑验证组,都产生了质疑结果。 更方便的是,在你到场时,按个案情况来弥补这种障碍,而不是试图在一件大事中书写整个数据检索轨道,而那件东西是一条可以维持的梦.。

As it stands all your test code that is verifying function or validating how the site works is going to be tightly coupled to a big monolithic piece that fetches the data from the db. that creates a lot of dependencies and makes your test code hard to maintain. If you deal with things in a more modular way, where each validation step retrieves just the data it needs, then it s a lot easier to expand or modify your test code as the site or database changes.

如果你有一阵列,有一栏名,那么你可以随行阵,并 build立一 has:

Hash[column_names.zip( row )]

不过,我建议对此采用积极记录。

This should work

connection = OCI8.new( usrname ,  pwd , //host:portno/sid ) 

cursor = connection.exec(("SELECT BI_PREFIX ...")
cols = cursor.get_col_names

while r = cursor.fetch
  $BI_PREFIX=r[cols.index( BI_PREFIX )].to_s
  ...
end




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...