Is the database cursor not working correctly?

Hello, all
I got a problem when retrieving all the key/data pairs from a database by using the cursor. After some debugging, I realized that the problem is due to the reuse of a DatabaseEntry object to get data from multiple entries. For example, there are two records in a database
key | data
1 "aaaaaaaa"
2 "bbbb"
If I want to know the length of data for each key/data pair, I expected the returning result would be:
the length of key 1's data is 8
the length of key 1's data is 5
However, when I use a cursor to retrieve the result back, it returns the following result:
the length of key 1's data is 8
the length of key 1's data is 8
The code to reproduce the incorrect result is attached below. One way to fix this code is to new a DatabaseEntry object within the loop. But the following code come from the berkeley db documentation (http://www.oracle.com/technology/documentation/berkeley-db/db/gsg/JAVA/Positioning.html). So I think it is good to report this problem.
OS: ubuntu
DB version: 4.7
Programming language: Java
import com.sleepycat.db.*;
import java.io.*;
public class TestDB {
     public static final int byteArrayToInt(byte [] b) {
          return (b[0] << 24)
          + ((b[1] & 0xFF) << 16)
          + ((b[2] & 0xFF) << 8)
          + (b[3] & 0xFF);
     public static final byte[] intToByteArray(int value) {
          return new byte[]{
                    (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) };
     public static void main(String[] args) {
          Database myDatabase = null;
          Cursor cursor = null;     
          try {
          // Open the database. Create it if it does not already exist.
          DatabaseConfig dbConfig = new DatabaseConfig();
          dbConfig.setAllowCreate(true);
          dbConfig.setType(DatabaseType.HASH);
          myDatabase = new Database ("/tmp/cosmos/sampleDatabase.db",
          null,
          dbConfig);
          int key1 = 0;
          byte[] data1 = "aaaaaaaa".getBytes();          
          DatabaseEntry dbKey1 = new DatabaseEntry(intToByteArray(key1));
          DatabaseEntry dbData1 = new DatabaseEntry(data1);
          myDatabase.put(null, dbKey1, dbData1);
          int key2 = 1;
          byte[] data2 = "bbbbb".getBytes();           
          DatabaseEntry dbKey2 = new DatabaseEntry(intToByteArray(key2));
          DatabaseEntry dbData2 = new DatabaseEntry(data2);
          myDatabase.put(null, dbKey2, dbData2);
          cursor = myDatabase.openCursor(null, null);
          DatabaseEntry foundKey = new DatabaseEntry();
          DatabaseEntry foundData = new DatabaseEntry();
          while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) ==
          OperationStatus.SUCCESS) {
               int key = Util.byteArrayToInt(foundKey.getData());
          System.out.println("the length of Key " + key + "'data is " + foundData.getData().length);
          } catch (DatabaseException dbe) {
          // Exception handling goes here
          } catch (FileNotFoundException fnfe) {
          // Exception handling goes here
          } finally {
               try {
                    cursor.close();
                    myDatabase.close();
               } catch (DatabaseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
}

Hi,
When a DatabaseEntry is reused when doing retrieval we internally use realloc to reuse the memory, hence the behavior you've observed.
You should use the DatabaseEntry.getSize() method to determine the byte size of the data array. This will provide the correct results you're looking for, as after retrieving an entry we internally set in the size of the retrieved DBT:
System.out.println("the length of Key " + key + "'data is " + foundData.getSize());
Regards,
Andrei

Similar Messages

  • I bought an IPAD air from state and the screen is not working correctly and right now i'm in Egypt ,can i send my ipad to service in Egypt and the warranty will work in Egypt or what?

    i bought an IPAD air (32 gb wifi only) from USA just one month ago and the screen is not working correctly(picture attached) and right now i'm in Egypt ,can i send my ipad to service in Egypt and the warranty will work in Egypt or what?

    Have you tried a soft-reset to see if that fixes it ? You might be able to get warranty service in Egypt for your iPad, but the warranty includes :
    IMPORTANT RESTRICTION FOR iPHONE AND iPAD SERVICE.
    Apple may restrict warranty service for iPhone and iPad to the country where Apple or its Authorized Distributors originally sold the device.
    As it's wifi-only you should have a better chance of it being serviced under warranty, but there are no guarantees, it might depend upon the repairer.
    Egypt authorised service providers : https://locate.apple.com/eg/en/

  • I just to a new iphone5s, the headphones are not working correctly. I can hear the music but I can't pause or play with the button. Will apple replace them?

    I just bought a new iphone5s, the headphones are not working correctly. I can hear the music but I can't pause or play with the button. Will apple replace them?

    Hi there,
    It sounds like your purchased content is not transfering via a flash drive. It may be easier to re-download this contnet directly from iTunes. Take a look at the article below for mor information.
    Downloading past purchases from the App Store, iBookstore, and iTunes Store
    http://support.apple.com/kb/ht2519
    -Griff W.

  • I am having problems with my Canon S100 camera. The shutter is not working correctly.

    I hope this is the right place to post a complaint about my Canon S100 camera's shutter not working correctly.  My daughter purchased two of these cameras, one for me, and one for herself.  Both cameras are having the same problem.  I hope Canon can help us with this serious problem.  What good is a camera if it does not take pictures?

    Stephen (Canon),
    I find it quite surprising that you do not respond to 28 pages of comments regarding this issue on the other thread and yet you directly comment here.
    Seems to me that the problem arises after significant use as the contacts in the shutter switch corrode and do not get the contact that they once did.  With many users this occurs just outside the 1yr warranty. However, it is clear that this is a design fault as opposed to natural 'wear and tear' and therefore I would expect that a company with Canon's reputation for excellent service would offer free repairs.
    Can you please confirm if Canon are preparing a response on this issue?
    Regards,
    Neil Walker

  • When building an iPad HTML5 app, why does the splash page not work correctly in landscape mode?

    I have recently completed an HTML5 web app for the ipad and one of the most challenging things about the process was getting the splash screen to work.
    I have now succesfully completed the job and the splash screen works, but only under the bizarre condition that the app is first launched in portrait mode. This seems pretty crazy as most people using the ipad for browsing do so in landscape mode.
    Has anyone come across this problem and resolved it?
    cheers

    yes, bare in mind this is an HTML5 Web App and not a native app. so yes I have included code for both landscape and portrait modes.
    I install the web app and open in portrait, it works. then close and open in landscape, it works.
    if i install the app and open in landscape FIRST... then no splash screen ever works.
    This isn't just my problem. I've read about in several forums over the past few days while investigating.
    thanks

  • TA24326 How to tell if Software Update for Windows is working correctly when no updates are available  --- the link is not working correctly.

    The link in the article does not go to the answer. It goes to a generic apple site.

    Well, the article is archived meaning it is no longer valid for the current version of iTunes.
    If you want the current version of iTunes... go to apple.com/itunes/download.

  • Selecting a one row from the database is not working

    Hi all I am having a small problem with getting a row from my database to show up. Here is the code for the search that I want to do
                            String item; String columnName; String result; columnName = (String)searchBox.getSelectedItem(); item = searchField.getText().trim(); String tableName = "employee"; if(item == null || item.equals("")){ System.out.println("No Text entered"); }else{ try { result = sql.retrieve_From_Database(columnName, tableName, item); System.out.println(result); } catch (SQLException e1) { sql.displayErrors(e1); }
    Here is the code for the retrieve_From_Database function.
    protected String retrieve_From_Database(String columnName, String tableName, String item) throws SQLException{ ResultSet rs = null; Statement sm = null; sm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = sm.executeQuery("select * from " + tableName + " where " + columnName + " = " + item); result = rs.getString(columnName); if(result == null){ System.out.println("Error in retrieve"); return new String("No results in DB"); }else{ System.out.println(result); return result; } }
    When I run the first code I get an error of SQL Exception: Unknown column 'anthony' in 'where clause'. But yet when I run the query of
    Select * from employee where FirstName = 'anthony'
    within the mysql commandline I get the proper result. So my question is why when running this in the command line do I get the proper result but when running the code I do not?

    jo**** wrote:
    Hi. The problem is clear in the original post. The OP is trying to use PreparedStatement
    parameters to fill out general portions of the SQL query, instead of only data values.
    He is hoping to put in a column name. That will never work. Only data values are settable.
    HTH,
    Joe WeinsteinActually, no. There's no PreparedStatement there at all. It appears that his problem is that, while the working query entered directly in the client is
    Select * from employee where FirstName = 'anthony'the query he's constructing in his code is
    Select * from employee where FirstName = anthonySince there are no quotes around anthony, it's treated as a column name.
    If he had properly used PreparedStatment as suggested in reply 1, that would not be an issue. My guess, however, is that when he claims he's using PS, it's as Tolls suggested a couple of posts ago--he's simply replacing the Statement with a PreparedStatement, but not using the parameters as suggested.
    The wrong thing for OP to do at this point is see my above comment about the quotes and explicitly add them in the query string. The right thing to do, of course, is to actually use PS's parameters.
    Edited by: jverd on Oct 11, 2010 10:08 AM

  • In the search feature of the phone, does not work correctly

    In the search feature of the phone, when typing a first name or last name, why does the search start with the letter A?  For example, I type ME and the first name that pops up is Cecil.  Cecil is in my contact list but not what I am searching for.

    I believe when using the search feature, it searches more fields than just the first and last name. See if Cecil has something in his contact card that contains MA, such as an address, company name, etc. Otherwise, I generally do not find any problems using search.

  • The APP STORE app on the iPhone 4S not working correctly for UPDATES now.

    It used to be, when I had APPS that needed to be updated, I would see a badge on the APP STORE icon with the number of apps that needed to be updated. Then I could tap on the icon for the APP STORE, and I would see the UPDATE button showing the badge as well. I would also see the actual apps needing updating on screen.
    NOW, even though there are APPS that need updating, there is NO badge on the APP STORE icon, there is NO BADGE on the UPDATE button, and no apps will appear on screen. If I tap the UPDATE button, after a few seconds, the apps WILL appear on screen, the badge WILL appear on the UPDATE button, and, if I go to the home screen, the APP STORE icon has the badge with the number.
    It is as if the apps that need updating do not automatically go to the APP STORE app anymore until I summon them by pressing the update button. This happened ever since I did an iPhone update from backup a few days ago.

    try hard resetting the device, holding the power button and the home button at the same time, and reset all settings (wont delete anything) settings>general>reset>reset all settings

  • Why is the home button not working correctly?

    I have reset the button by holding sleep key &amp; home button down, i have also tried to reset the home button by holding this down itself, it is still none responsive most if the time.  I have done this many times.
    It is also very difficult to get it to open the 'open apps bar', so i can close these down!!
    My ipad is only 4 days old and i feel frustrated to be having problems so soon!!!

    Calibrate Home Button
    1. Open a stock application like Calendar, Weather or Youtube
    2. Hold down the power button until the "slide to power off" control appears
    3. As soon as you see it, press the Home button and keep it pressed until the red slider disappears and the app is forced quit to the Home menu

  • TS1702 I know have to use the podcast app to listen to my podcast. And if course I have a issue and the app does not work correctly. I can not stream my podcast and have my phone locked, I have to listen to my podcast with my phone unlocked. Please help

    I hate the pOdcast app I can't listen to my podcast and have my phone locked. Is there anyway around this problem

    I hate the pOdcast app I can't listen to my podcast and have my phone locked. Is there anyway around this problem

  • Sorting not working correctly for date field in alv report

    Hi All,
    My report displays many rows also containing date type fields of bldat,budat .
    When I sort the report selecting field of type bldat budat the sorting is not correct for the year.
    Ex:
    Invoice doc dat
    01-25-2011
    01-21-2011
    02-02-2011
    10-25-2010
    11-20-2010
    If I use ascending then it is sorted as :
    Invoice doc dat
    01-21-2011
    01-25-2011
    02-02-2011
    10-20-2010
    10-25-2010
    Why the sorting is not working correct for year.(2010 records should have been first).
    The field wa_tab-bldat is of type char10.
    It is populated as wa_tab-bldat = bsak-bldat.
    Kindly suggest what can be done.

    The field wa_tab-bldat is of type char10
    Then what it does is correct.
    Refer to type datum...it will work

  • LaserJet Pr0 200 color MFP M276nw Touch Screen not Working correctly

    Hello, I'm having an issue with a LaserJet Pr0 200 color MFP M276nw Touch Screen not Working correctly. I have followed the instructions to reset the printer, but now am unable to select 'English' as my language because every time I select the 'English' option 'Spanish' is selected. I am then given two options to select 'Si' or 'No' both of which can not be selected on the touch screen?
    Anybody know of a way round this?

    Hi @dnhowes ,
    I see that you are having issues with the touch screen not working correctly. I will do my best to help you.
    Make sure the printer is connected directly to a wall outlet. (don't use a power hub or a surge protector) This ensures the printer is receiving full power and may help this situation.
    Was it the Nvram reset back to factory settings that you tried or a hard reset?
    Try selecting the option on the display a little higher above the icon to see if that helps. Some touch screens are finicky.
    I find that on the Lab printers when I am selecting different menus.
    If the issue continues, please call our technical support at 800-474-6836 for further assistance. If you live outside the US/Canada Region, please click the link below to get the support number for your region.
    http://www8.hp.com/us/en/contact-hp/ww-phone-assist.html
    Have a nice day!
    Thank You.
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos Thumbs Up" on the right to say “Thanks” for helping!
    Gemini02
    I work on behalf of HP

  • Forecast splitting not working correctly

    Hello
    I have a problem with the forecast splitting not working correctly.  We are using 2M->W     2 MONTH IN WEEKS     0  GLEI and most of the time this appears to work fine but there are occasions where the forecast is split for May, did not split Jun but split July.
    E.g. 
    06.05.2008     IndReq     VSF               9,424-
    12.05.2008     IndReq     VSF               20,845-
    19.05.2008     IndReq     VSF               30,925-
    27.05.2008     IndReq     VSF               24,740-
    02.06.2008     IndReq     VSF               134,257-
    01.07.2008     IndReq     VSF               25,771-
    07.07.2008     IndReq     VSF               32,213-
    14.07.2008     IndReq     VSF               32,214-
    21.07.2008     IndReq     VSF               32,213-
    28.07.2008     IndReq     VSF               25,770-     
    Do any of you Guru's have any idea why this would happen?
    Many thanks
    Sue

    I checked it in MD67
    it is working fine.
    In MD61 - my demand is 800 each for June and July. it hjas splited as below
    system is dividing the total qty on daily basis and then multiplying by no days in that week covered by requirement
    W 22.2008             26.667
    W 23.2008            186.667
    W 24.2008            186.666
    W 25.2008            186.667
    W 26.2008            186.667
    W 27.2008             26.666
    W 27.2008            154.839
    W 28.2008            180.645
    W 29.2008            180.646
    W 30.2008            180.645
    W 31.2008            103.225

  • Why the games are not working properly in my ipad after installing iOS7

    Why games are not working properly in my ipad in my iOS 7

    The games you mention would have been written and therefore designed for the previous version of iOS - you've most likely had a lot of updates lately in the run-up to the release of iOS 7 and if these games haven't had an update then chances are these will need to be adapted to iOS 7 by the developers if not working correctly.
    Regards,
    Steve

Maybe you are looking for

  • How to avoid safety warning when opening a pdf from an link in another pdf?

    I make pdf in Acrobat with links to other pdf files av have made. When i try to click a link to open the pdf, i get the safety question if trust it or if i will block it. How to avoid that question all the time?

  • How do I play videos without using adobe flash player?

    I have a galaxy note 8.0 tablet. I am aware that adobe flashplayer is not compatible. I have downloaded adobe air, but I still am unable to play videos for school. Is there any other ways around this issue? Thank you for your help.

  • PAYR table

    Hi Everyone, I have an issue regarding the table PAYR, when run in transaction se16 and after entering the vendor numbers it shows only the payments made by check till 2004 and not after that since the payment to that vendor was changed to wire. It i

  • PAGINATION ERROR

    Hi, I'm trying to get this pagination code functioning but I keep getting an error on my query. The error says: Microsoft Access Driver] Syntax error (missing operator) in query expression 'titles LIMIT 0' and I'm completely stumped, I pasted my code

  • How to remove disabled plugin in Firefox

    I have three Firefox Plugins which are disabled, but no means to remove same, and they do not show up Microsoft Uninstall or Change a Program Panel.