Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:31:33+00:00 2026-06-09T06:31:33+00:00

I need help in how to retrieve data from SQLite database and put into

  • 0

I need help in how to retrieve data from SQLite database and put into spinner.
Plus, I can’t open my android app and the LogCat says “No such column:name”. I have even check with my DBAdapter classes and non-DBAdapter classes.

I have two DBAdapter classes(InfoDBAdapter.java and BuddiesDBAdapter.java) both link to my PersonalInfo.java class.

Below are the codes for retrieving data from database and put into spinner(PersonalInfo.class).

InfoDBAdapter infoDB = new InfoDBAdapter(this);

//BuddiesDBAdapter buddyDB = new BuddiesDBAdapter(this);

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
setContentView(R.layout.info);

BuddiesDBAdapter buddyDB = new BuddiesDBAdapter(this);

buddyDB.open();
Cursor c = buddyDB.getAllContacts();

//Create an array to specify which fields want to display
    String[] from = new String[] {"name"};

    //Create an array of the display item want to bind our data to
    int[] to = new int[] {R.id.fName};

    nameSpinner = (Spinner) findViewById(R.id.nameSpinner);

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    nameSpinner.setAdapter(adapter);

    buddyDB.close();

and these are the insert codes for inserting informaton into database.

btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    infoDB.open();
                    long id;

                    Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
                    String NameValue = nameSpinner.getSelectedItem().toString();

                    EditText txtDate = (EditText) findViewById(R.id.txtDate);
                    String DateValue = txtDate.getText().toString();

                    EditText txtType = (EditText) findViewById(R.id.txtType);
                    String TypeValue = txtType.getText().toString();

                    EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
                    String LikesValue = txtLikes.getText().toString();

                    EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
                    String DislikesValue = txtDislikes.getText().toString();

                    id = infoDB.insertContact(NameValue, DateValue, TypeValue, LikesValue, DislikesValue);
                    infoDB.close();

                    Toast.makeText(getBaseContext(), 
                            "Your information is saved successfully!", Toast.LENGTH_SHORT).show();

                }
            });

Below are the two DBAdapter classes

InfoDBAdapter.java

public class InfoDBAdapter
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_DATE = "date";
    public static final String KEY_TYPE = "type";
    public static final String KEY_LIKES = "likes";
    public static final String KEY_DISLIKES = "dislikes";

    private static final String TAG = "InfoDBAdapter";

    private static final String DATABASE_NAME = "anniversary";
    private static final String DATABASE_TABLE = "friends";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE = "create table friends(_id integer primary key autoincrement, "
            + "name text not null, date text not null, type text not null, likes text not null, dislikes text not null);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public InfoDBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try
            {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }// end onCreate()

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS friends");
            onCreate(db);
        }// end onUpgrade()

    }// end DatabaseHelper

        public InfoDBAdapter open() throws SQLException
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }// end open()

        public void close()
        {
            DBHelper.close();
        }// end close()

        public long insertContact(String name, String date, String type, String likes, String dislikes)
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_NAME, name);
            initialValues.put(KEY_DATE, date);
            initialValues.put(KEY_TYPE, type);
            initialValues.put(KEY_LIKES, likes);
            initialValues.put(KEY_DISLIKES, dislikes);
            return db.insert(DATABASE_TABLE, null, initialValues);
        }// end insertContact()

        public boolean deleteContact(long rowId)
        {
            return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
        }// end deleteContact()

        public Cursor getAllContacts()
        {
            return db.query(DATABASE_TABLE, new String[] 
                    { KEY_ROWID, KEY_NAME, KEY_DATE, KEY_TYPE, KEY_LIKES, KEY_DISLIKES }, null, null, null, null, null);
        }// end getAllContacts()

        public Cursor getContact(long rowId) throws SQLException
        {
            Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID, KEY_NAME, KEY_DATE, KEY_TYPE, KEY_LIKES, KEY_DISLIKES }, KEY_ROWID + "=" + rowId,
                    null, null, null, null, null);
            if (mCursor != null)
            {
                mCursor.moveToFirst();
            }
            return mCursor;
        }// end getContact()

        public boolean updateContact(long rowId, String name, String date, String type, String likes, String dislikes)
        {
            ContentValues args = new ContentValues();
            args.put(KEY_NAME, name);
            args.put(KEY_DATE, date);
            args.put(KEY_TYPE, type);
            args.put(KEY_LIKES, likes);
            args.put(KEY_DISLIKES, dislikes);
            return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
        }// end updateContact()
}//end DBAdapter

BuddiesDBAdapter.java

public class BuddiesDBAdapter
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "buddy_name";
    private static final String TAG = "BuddiesDBAdapter";

    private static final String DATABASE_NAME = "anniversary";
    private static final String DATABASE_TABLE = "buddiesList";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE = "create table namesList(_id integer primary key autoincrement, "
            + "buddy_name text not null);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public BuddiesDBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try
            {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }// end onCreate()

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS namesList");
            onCreate(db);
        }// end onUpgrade()

    }//end DatabaseHelper class

    public BuddiesDBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }// end open()

    public void close()
    {
        DBHelper.close();
    }// end close()

    public long insertContact(String buddy_name)
    {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, buddy_name);
        return db.insert(DATABASE_TABLE, null, values);         
    }//end insertContact()


    public boolean deleteContact(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }// end deleteContact()


    public Cursor getAllContacts()
    {
        return db.query(DATABASE_TABLE, new String[] 
                { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
    }// end getAllContacts()

    public Cursor getContact(long rowId) throws SQLException
    {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId,
                null, null, null, null, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }// end getContact()

    public boolean updateContact(long rowId, String buddy_name)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, buddy_name);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }//end updateContact()
}

I need help with this. Any help would be appreciated. Thanks alot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:31:35+00:00Added an answer on June 9, 2026 at 6:31 am

    The above answer is correct. What is confusing is that you have two different db adapters and the table name/column names are different.

    For maintainability, I would not hard code the column names, but instead use

    String[] from = new String[] {BuddiesDBAdapter.KEY_NAME};
    

    Hope this helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to retrieve data from within a specific date range.Anybody can help me
I retrieve few data from database then store it into hashmap and display it
I need to write on a TextView dynamically, actually my app retrieve data from
I need to access some data from an MS Access database and retrieve some
Hello I need to retrieve data from database via an ajax call. I am
Need help writing a script downloads data from google insight using c# this is
i need to retrive some data from table registration of database to show.aspx page
I am new to iPhone programming,need some Help, I am using SQLITE Database In
I have been searching for how to retrieve binary data from the database. My
I need to execute a query To retrieve data from multiple tables but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.