problem with sgl

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

problem with sgl

Postby georpapa » Thu Oct 08, 2009 7:27 am

hello, i have a small problem retrieving a specific field from a row in a table.

i want to have a select username from user where id="1"; for example in the sql android.

the code i have for the database is from the notepad tutorial and i have modified it a bit

when i call mDbHelper.fetchnotes2("nikos");
it occurs an error, i saw that this error exist when i run the specific query:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Cursor user=mDb.query(true,DATABASE_TABLE, new String[] {KEY_ROWID,KEY_USERNAME,
  2.  
  3.             KEY_ΝΑΜΕ,KEY_LASTNAME, KEY_IP}, KEY_USERNAME + "=" + username,null,
  4.  
  5.               null,null,null, null);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



what i do wrong?
and how i will take the value i want from this?
with this code?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String data =user.getString(user.getColumnIndexOrThrow(NotesDbAdapter.KEY_USERNAME));
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


ty

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.android.maptest;
  2.  
  3.  
  4.  
  5. import android.content.ContentValues;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.database.Cursor;
  10.  
  11. import android.database.SQLException;
  12.  
  13. import android.database.sqlite.SQLiteDatabase;
  14.  
  15. import android.database.sqlite.SQLiteOpenHelper;
  16.  
  17. import android.util.Log;
  18.  
  19.  
  20.  
  21.  
  22.  
  23. public class NotesDbAdapter {
  24.  
  25.  
  26.  
  27.  
  28.  
  29.     public static final String KEY_ΝΑΜΕ = "name";
  30.  
  31.     public static final String KEY_USERNAME = "username";
  32.  
  33.     public static final String KEY_LASTNAME = "lastname";
  34.  
  35.     public static final String KEY_IP = "ip";
  36.  
  37.     public static final String KEY_ROWID = "_id";
  38.  
  39.  
  40.  
  41.     private static final String TAG = "NotesDbAdapter";
  42.  
  43.     private DatabaseHelper mDbHelper;
  44.  
  45.     private SQLiteDatabase mDb;
  46.  
  47.    
  48.  
  49.     /**
  50.  
  51.      * Database creation sql statement
  52.  
  53.      */
  54.  
  55.     private static final String DATABASE_CREATE =
  56.  
  57.             "create table users (_id integer primary key autoincrement, "
  58.  
  59.                     + "username text not null, name text not null, lastname text not null, ip text not null);";
  60.  
  61.  
  62.  
  63.     private static final String DATABASE_NAME = "userdata";
  64.  
  65.     private static final String DATABASE_TABLE = "users";
  66.  
  67.     private static final int DATABASE_VERSION = 2;
  68.  
  69.  
  70.  
  71.     private final Context mCtx;
  72.  
  73.  
  74.  
  75.     private static class DatabaseHelper extends SQLiteOpenHelper {
  76.  
  77.  
  78.  
  79.         DatabaseHelper(Context context) {
  80.  
  81.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  82.  
  83.         }
  84.  
  85.  
  86.  
  87.         @Override
  88.  
  89.         public void onCreate(SQLiteDatabase db) {
  90.  
  91.  
  92.  
  93.             db.execSQL(DATABASE_CREATE);
  94.  
  95.         }
  96.  
  97.  
  98.  
  99.         @Override
  100.  
  101.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  102.  
  103.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  104.  
  105.                     + newVersion + ", which will destroy all old userdata");
  106.  
  107.             db.execSQL("DROP TABLE IF EXISTS users");
  108.  
  109.             onCreate(db);
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.  
  116.  
  117.     /**
  118.  
  119.      * Constructor - takes the context to allow the database to be
  120.  
  121.      * opened/created
  122.  
  123.      *
  124.  
  125.      * @param ctx the Context within which to work
  126.  
  127.      */
  128.  
  129.     public NotesDbAdapter(Context ctx) {
  130.  
  131.         this.mCtx = ctx;
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     /**
  138.  
  139.      * Open the notes database. If it cannot be opened, try to create a new
  140.  
  141.      * instance of the database. If it cannot be created, throw an exception to
  142.  
  143.      * signal the failure
  144.  
  145.      *
  146.  
  147.      * @return this (self reference, allowing this to be chained in an
  148.  
  149.      *         initialization call)
  150.  
  151.      * @throws SQLException if the database could be neither opened or created
  152.  
  153.      */
  154.  
  155.     public NotesDbAdapter open() throws SQLException {
  156.  
  157.         mDbHelper = new DatabaseHelper(mCtx);
  158.  
  159.         mDb = mDbHelper.getWritableDatabase();
  160.  
  161.         return this;
  162.  
  163.     }
  164.  
  165.    
  166.  
  167.     public void close() {
  168.  
  169.         mDbHelper.close();
  170.  
  171.     }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.     /**
  178.  
  179.      * Create a new note using the title and body provided. If the note is
  180.  
  181.      * successfully created return the new rowId for that note, otherwise return
  182.  
  183.      * a -1 to indicate failure.
  184.  
  185.      *
  186.  
  187.      * @param title the title of the note
  188.  
  189.      * @param body the body of the note
  190.  
  191.      * @return rowId or -1 if failed
  192.  
  193.      */
  194.  
  195.     public long createNote(String username, String name, String lastname, String ip) {
  196.  
  197.         ContentValues initialValues = new ContentValues();
  198.  
  199.         initialValues.put(KEY_USERNAME, username);
  200.  
  201.         initialValues.put(KEY_ΝΑΜΕ, name);
  202.  
  203.         initialValues.put(KEY_LASTNAME, lastname);
  204.  
  205.         initialValues.put(KEY_IP, ip);
  206.  
  207.        
  208.  
  209.  
  210.  
  211.         return mDb.insert(DATABASE_TABLE, null, initialValues);
  212.  
  213.     }
  214.  
  215.  
  216.  
  217.     /**
  218.  
  219.      * Delete the note with the given rowId
  220.  
  221.      *
  222.  
  223.      * @param rowId id of note to delete
  224.  
  225.      * @return true if deleted, false otherwise
  226.  
  227.      */
  228.  
  229.     public boolean deleteNote(long rowId) {
  230.  
  231.  
  232.  
  233.         return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
  234.  
  235.     }
  236.  
  237.  
  238.  
  239.     /**
  240.  
  241.      * Return a Cursor over the list of all notes in the database
  242.  
  243.      *
  244.  
  245.      * @return Cursor over all notes
  246.  
  247.      */
  248.  
  249.     public Cursor fetchAllusers() {
  250.  
  251.  
  252.  
  253.         return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME,KEY_ΝΑΜΕ,
  254.  
  255.                 KEY_LASTNAME,KEY_IP}, null, null, null, null, null);
  256.  
  257.     }
  258.  
  259.  
  260.  
  261.     /**
  262.  
  263.      * Return a Cursor positioned at the note that matches the given rowId
  264.  
  265.      *
  266.  
  267.      * @param rowId id of note to retrieve
  268.  
  269.      * @return Cursor positioned to matching note, if found
  270.  
  271.      * @throws SQLException if note could not be found/retrieved
  272.  
  273.      */
  274.  
  275.     public Cursor fetchuser(long rowId) throws SQLException {
  276.  
  277.  
  278.  
  279.         Cursor mCursor =
  280.  
  281.  
  282.  
  283.                 mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_USERNAME,
  284.  
  285.                         KEY_ΝΑΜΕ,KEY_LASTNAME, KEY_IP}, KEY_ROWID + "=" + rowId, null,
  286.  
  287.                         null, null, null, null);
  288.  
  289.         if (mCursor != null) {
  290.  
  291.             mCursor.moveToFirst();
  292.  
  293.         }
  294.  
  295.         return mCursor;
  296.  
  297.  
  298.  
  299.     }
  300.  
  301.    
  302.  
  303.     public String fetchuser2(String username) throws SQLException {
  304.  
  305.         Cursor user=mDb.query(true,DATABASE_TABLE, new String[] {KEY_ROWID,KEY_USERNAME,
  306.  
  307.               KEY_ΝΑΜΕ,KEY_LASTNAME, KEY_IP}, KEY_USERNAME + "=" + username,null,
  308.  
  309.             null,null,null, null);
  310.  
  311.        
  312.  
  313.         String data =user.getString(user.getColumnIndexOrThrow(NotesDbAdapter.KEY_USERNAME));
  314.  
  315.         String data="nikos2";
  316.  
  317.         return data;
  318.  
  319.  
  320.  
  321.     }
  322.  
  323.    
  324.  
  325.     public boolean updateNote(long rowId, String username, String name, String lastname, String ip) {
  326.  
  327.         ContentValues args = new ContentValues();
  328.  
  329.         args.put(KEY_USERNAME, username);
  330.  
  331.         args.put(KEY_ΝΑΜΕ, name);
  332.  
  333.         args.put(KEY_LASTNAME, lastname);
  334.  
  335.         args.put(KEY_IP, ip);
  336.  
  337.  
  338.  
  339.         return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
  340.  
  341.     }
  342.  
  343.    
  344.  
  345. }
  346.  
  347.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4
georpapa
Freshman
Freshman
 
Posts: 9
Joined: Tue Oct 06, 2009 7:22 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests