简体   繁体   中英

Data not inserted into SQLite database on android?

I have developed an Android database application.
The problem is that when I click the add button then a message shows that data was successfully added.
But the data is not inserted into the database.
I then open the database with SQLite viewer and there I can't find any of the data I inserted...

Register page code :

package com.example.dbapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Register extends Activity {

Button add;
TextView b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    add = (Button) findViewById(R.id.btnReg);
    b   = (TextView) findViewById(R.id.etName);
    c   = (TextView) findViewById(R.id.etOccu);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                String name   = b.getText().toString();
                String occ    = c.getText().toString();
                DataCon entry = new DataCon(Register.this);
                entry.open();
                entry.createEntry(name, occ);
                entry.close();
                Toast toast   = Toast.makeText(getApplicationContext(), "Sucess", Toast.LENGTH_SHORT);
                toast.show();
            }
            catch(Exception e) {
                String err  = e.toString();
                Toast toast = Toast.makeText(getApplicationContext(), err,           Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    });
}

Database Connection code :

package com.example.dbapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataCon {
public static final String rowid = "_id";
public static final String name  = "persons_name";
public static final String hot   = "persons_hotness";

private static final String DB_NAME  = "myDB";
private static final String DB_TABLE = "tbl_me";
private static final int DB_VER      = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid + 
                " INTEGER PRIMARY KEY AUTOINCREMENT, " + name + "TEXT              NOT    NULL, " +
                hot + " TEXT NOT NULL);");
    }

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

}

public DataCon(Context c) {
    ourContext = c;
}

public DataCon open() {
    ourHelper   = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close() {
    ourHelper.close();
}

public long createEntry(String name2, String occ) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(name, name2);
    cv.put(hot, occ);
    return ourDatabase.insert(DB_TABLE, null, cv);
}
}

Now how can I solve the problem? thanks

An error in your sql. You haven't provide space between column name and type. Correct one should be

db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid + 
" INTEGER PRIMARY KEY AUTOINCREMENT, " + name + " TEXT NOT NULL, " +
                      --------------------------- ^ 
hot + " TEXT NOT NULL);");`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM