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 8643901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:09:52+00:00 2026-06-12T12:09:52+00:00

It seems I can’t insert data into my database and I don’t know where

  • 0

It seems I can’t insert data into my database and I don’t know where is the problem.
Here’s the database code:

public class Database {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "Name";
    public static final String KEY_PERIOD = "Period";
    public static final String KEY_FREQUENCY = "Frequency";
    public static final String KEY_RECIEVINGTIME = "Recieving_time";
    public static final String KEY_STATS = "Stats";

    private static final String DATABASE_NAME = "Reports";
    private static final String DATABASE_TABLE = "Table1";
    private static final int DATABASE_VERSION = 1;

    private DbHelper myHelper;
    private final Context myContext;
    private SQLiteDatabase myDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_NAME + " TEXT NOT NULL, " +
                    KEY_PERIOD + " TEXT NOT NULL, " +
                    KEY_FREQUENCY + " TEXT NOT NULL, " +
                    KEY_RECIEVINGTIME + " TEXT NOT NULL, " +
                    KEY_STATS + " TEXT NOT NULL);"
                    );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }
    }

    public Database(Context c){
        myContext = c;
    } 

    public Database open(){
        myHelper = new DbHelper(myContext);
        myDatabase = myHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        myDatabase.close();
    }

    public void writeEntry(String name, long period, long frequency, long recievingTime, String stats){
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_PERIOD, period);
        cv.put(KEY_FREQUENCY, frequency);
        cv.put(KEY_RECIEVINGTIME, recievingTime);
        cv.put(KEY_STATS, stats);
        myDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public Cursor query(String[] columns, String selection, String[] selectionArgs, String groupBy) {
        // TODO Auto-generated method stub
        Cursor c = myDatabase.query(DATABASE_TABLE, columns, selection, selectionArgs, groupBy, null, null);
        return c;
    }   
}

I try to insert data with this method:

public void save(){
    sharedPrefs = context.getSharedPreferences(SPNAME, 0);
    Log.v(" info", sharedPrefs.getAll().toString());
    Database database = new Database(context);
    database.open();
    database.writeEntry(
            sharedPrefs.getString(NAME, "No data was found"), 
            sharedPrefs.getLong(PERIOD, 0),
            sharedPrefs.getLong(FREQUENCY, 0), 
            sharedPrefs.getLong(RECIEVINGTIME, 0), 
            sharedPrefs.getString(STATS, "No data was found")
            );
    database.close();

    editor.remove(NAME);
    editor.remove(FREQUENCY);
    editor.remove(RECIEVINGTIME);
    editor.remove(PERIOD);
    editor.remove(FREQUENCY);
    editor.remove(STATS);
    editor.commit();
}

And I retrieve data with this method:

 public ReportItem getReportInfo(String rowId){
    ReportItem item = new ReportItem();
    database = new Database(context);
    database.open();
    cursor = database.query(new String[]{
            Database.KEY_NAME,
            Database.KEY_PERIOD,
            Database.KEY_FREQUENCY,
            Database.KEY_RECIEVINGTIME,
            Database.KEY_STATS}, Database.KEY_ROWID +" = "+rowId, null, null);

    if(cursor.moveToFirst()){
        item.setName(cursor.getString(cursor.getColumnIndex(Database.KEY_NAME)));
        item.setPeriod(cursor.getLong(cursor.getColumnIndex(Database.KEY_PERIOD)));
        item.setFrequency(cursor.getLong(cursor.getColumnIndex(Database.KEY_FREQUENCY)));
        item.setReceivingTime(cursor.getLong(cursor.getColumnIndex(Database.KEY_RECIEVINGTIME)));
        item.setStats(cursor.getString(cursor.getColumnIndex(Database.KEY_STATS)));         
    }
    cursor.close(); 
    database.close();   
    Log.v("Data from database", item.toString());
    return item;
}

In save() method before inserting data to db log shows:

09-27 17:19:54.973: V/info(31638): <!>com.dailyreports.ainius.Report 75<!> {Period=86400000, Name=report1, Stats=23 24 26 , Frequency=86400000, RecievingTime=62340000}

But in the getReportInfo(String) method after retrieving the same info from db log shows:

09-27 17:20:06.263: V/Data from database(31638): <!>com.dailyreports.ainius.Report 117<!>  0 0 0 

Any suggestions? Thanks in advance.

  • 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-12T12:09:53+00:00Added an answer on June 12, 2026 at 12:09 pm

    You don’t have an entry that matches rowId:

    Database.KEY_ROWID +" = "+rowId
    

    So in getReportInfo():

    1. if(cursor.moveToFirst()) returns false,
    2. ReportItem item doesn’t receive new values,
    3. item returns the default values: empty Strings and 0l longs.

    Double-check your value of rowId.

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

Sidebar

Related Questions

It seems JAXB can't read what it writes. Consider the following code: interface IFoo
It seems like I can't catch exceptions in my code when the method was
I have this simple example I can't seems to get working : MERGE INTO
Is my code correct? It seems can compile but does not work properly.. CString
it seems I can no longer compile my code in GCC post Ubuntu 11.10
Going through the man page of tcpdump here It seems kernel can drop the
Here is my implementation of a spin lock, but it seems it can not
It seems both can be overloaded, but somebody said not..... What's the case?
It seems I can't easily have an XSD declaration for this simple XML <root>
I am using cezPDF library in CodeIgniter 2 But it seems it can't print

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.