我的数据未保存在数据库中

时间:2018-08-17 15:51:54

标签: android database sqlite

我的数据没有保存在我的数据库中。 当我输入数据并单击保存按钮,然后转到数据文件夹并打开数据库时,我了解到我的数据没有保存。 现在我想知道我的数据库有什么问题 (我已将设备植根)

box_2_dtent.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.box_2_dtent);
    contentHelper = new ContentHelper(box_2_dtent.this);
    final EditText cmpnm = (EditText)findViewById(R.id.act_2_cmpnm);
    final EditText cmptl = (EditText)findViewById(R.id.act_2_cmptel);
    final EditText arz = (EditText)findViewById(R.id.act_2_arz);
    final EditText bxcel = (EditText)findViewById(R.id.act_2_bxcel);
    final EditText bxch = (EditText)findViewById(R.id.act_2_bxch);
    final EditText bxclr = (EditText)findViewById(R.id.act_2_bxclr);
    final EditText bxcst = (EditText)findViewById(R.id.act_2_bxcst);
    final EditText bxds = (EditText)findViewById(R.id.act_2_bxds);
    final EditText bxex = (EditText)findViewById(R.id.act_2_bxex);
    final EditText bxgh = (EditText)findViewById(R.id.act_2_bxgh);
    final EditText bxgl = (EditText)findViewById(R.id.act_2_bxgl);
    final EditText bxkl = (EditText)findViewById(R.id.act_2_bxkl);
    final EditText bxknd = (EditText)findViewById(R.id.act_2_bxknd);
    final EditText bxmn = (EditText)findViewById(R.id.act_2_bxmn);
    final EditText bxmove = (EditText)findViewById(R.id.act_2_bxmove);
    final EditText bxnum = (EditText)findViewById(R.id.act_2_bxnum);
    final EditText bxzn = (EditText)findViewById(R.id.act_2_bxzn);
    final EditText bxsd = (EditText)findViewById(R.id.act_2_bxsd);
    final EditText tol = (EditText)findViewById(R.id.act_2_tol);
    final EditText ertefa = (EditText)findViewById(R.id.act_2_ertefa);
    final EditText date = (EditText)findViewById(R.id.act_2_date);
    final EditText prjnm = (EditText)findViewById(R.id.act_2_prjnm);
    final EditText bxps = (EditText)findViewById(R.id.act_2_bxps);
    final EditText bxpck = (EditText)findViewById(R.id.act_2_bxpck);
    final TextView percst = (TextView)findViewById(R.id.act_2_percst);
    final TextView finalcost = (TextView)findViewById(R.id.act_2_finalcost);
    Button btnres = (Button)findViewById(R.id.act_2_res);
    btnres.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            {
                content content = new content();
                content.cmpnm = cmpnm.getText().toString();
                content.tel = cmptl.getText().toString();
                content.arz = arz.getText().toString();
                content.cel = bxcel.getText().toString();
                content.chop = bxch.getText().toString();
                content.clr = bxclr.getText().toString();
                content.cost = bxcst.getText().toString();
                content.design = bxds.getText().toString();
                content.ext = bxex.getText().toString();
                content.gh = bxgh.getText().toString();
                content.gl = bxgl.getText().toString();
                content.kl = bxkl.getText().toString();
                content.bxknd = bxknd.getText().toString();
                content.mn = bxmn.getText().toString();
                content.move = bxmove.getText().toString();
                content.num = bxnum.getText().toString();
                content.zn = bxzn.getText().toString();
                content.profit = bxsd.getText().toString();
                content.tol = tol.getText().toString();
                content.ertefa = ertefa.getText().toString();
                content.date = date.getText().toString();
                content.prjnm = prjnm.getText().toString();
                content.pst = bxps.getText().toString();
                content.pack = bxpck.getText().toString();
                content.percost = percst.getText().toString();
                content.finalcost = finalcost.getText().toString();
                contentHelper.save(content);
                Toast.makeText(getApplication(), getString(R.string.savedatecomplte), Toast.LENGTH_LONG).show();
                cmpnm.setText("");
                cmptl.setText("");
                arz.setText("");
                bxcel.setText("");
                bxch.setText("");
                bxclr.setText("");
                bxcst.setText("");
                bxds.setText("");
                bxex.setText("");
                bxgh.setText("");
                bxgl.setText("");
                bxkl.setText("");
                bxknd.setText("");
                bxmn.setText("");
                bxmove.setText("");
                bxnum.setText("");
                bxzn.setText("");
                tol.setText("");
                ertefa.setText("");
                date.setText("");
                prjnm.setText("");
                bxps.setText("");
                bxpck.setText("");
                cmpnm.setText("");
                bxsd.setText("");

            }
        }
    });

content.java

package m_khalili.ir.boxmakingassistant;
public class content
{
Long id;
String cmpnm;
String bxknd;
String tol;
String arz;
String ertefa;
String cost;
String num;
String date;
String tel;
String design;
String zn;
String pst;
String chop;
String gl;
String mn;
String gh;
String kl,pack,move,ext,clr,cel,profit,finalcost,percost,prjnm;

}

ContentHelper.java

public class ContentHelper
{
dbhelper dbhelper;
public ContentHelper(Context context)
{
    dbhelper = new dbhelper(context);
}
public Long save(content content)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put("cmpnm",content.cmpnm);
    contentValues.put("bxknd",content.bxknd);
    contentValues.put("tol",content.tol);
    contentValues.put("arz",content.arz);
    contentValues.put("ertefa",content.ertefa);
    contentValues.put("cost",content.cost);
    contentValues.put("num",content.num);
    contentValues.put("date",content.date);
    contentValues.put("tel",content.tel);
    contentValues.put("design",content.design);
    contentValues.put("zn",content.zn);
    contentValues.put("pst",content.pst);
    contentValues.put("chop",content.chop);
    contentValues.put("gl",content.gl);
    contentValues.put("mn",content.mn);
    contentValues.put("gh",content.gh);
    contentValues.put("kl",content.kl);
    contentValues.put("pack",content.pack);
    contentValues.put("move",content.move);
    contentValues.put("ext",content.ext);
    contentValues.put("clr",content.clr);
    contentValues.put("cel",content.cel);
    contentValues.put("profit",content.profit);
    contentValues.put("finalcost",content.finalcost);
    contentValues.put("percost",content.percost);
    contentValues.put("prjnm",content.prjnm);

    dbhelper.opendb();
    Long a = dbhelper.db.insert(m_khalili.ir.boxmakingassistant.dbhelper.content,null,contentValues);
    dbhelper.closedb();
    return a;
}
}

dbhelper.java

public class dbhelper extends SQLiteOpenHelper
{
public static final String dbname = "datebase.db";
public static final int dbversion = 1;
public static final String content = "content";
SQLiteDatabase db;
public dbhelper(Context context)
{
    super(context, dbname, null, dbversion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
    sqLiteDatabase.execSQL("CREATE TABLE " + content + " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
            " cmpnm STRING, bxknd STRING, tol STRING NOT NULL, arz STRING NOT NULL, ertefa STRING NOT NULL," +
            " cost STRING NOT NULL, num STRING NOT NULL, date STRING, tell STRING, design STRING, zn STRING," +
            " pst STRING, chop STRING, gl STRING, mn STRING, gh STRING, kl STRING, pack STRING, move STRING," +
            " ext STRING, clr STRING, cel STRING, prf STRING, finalcost STRING, percost STRING, prjnm STRING)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
public void opendb()
{
    db = this.getWritableDatabase();
}
public void closedb()
{
    if(db.isOpen())
    {
        db.close();
    }
}
}

我做了一个白色的教程

1 个答案:

答案 0 :(得分:0)

我相信您的问题是ContentHelper.java中的某些列名与表的定义中的相应列名不匹配。

那是您会遇到诸如:-

的错误
08-18 23:05:11.449 1915-1915/so51893932.so51893932 E/SQLiteLog: (1) table content has no column named profit
08-18 23:05:11.450 1915-1915/so51893932.so51893932 E/SQLiteDatabase: Error inserting cel=d ertefa=t finalcost=z profit=r tol=s mn=n gl=k kl=l prjnm=v num=p cost=g arz=c clr=f chop=e cmpnm=a gh=j date=u pst=w zn=q pack=x ext=i bxknd=m tel=b percost=y move=o design=h
    android.database.sqlite.SQLiteException: table content has no column named profit (code 1): , while compiling: INSERT INTO content(cel,ertefa,finalcost,profit,tol,mn,gl,kl,prjnm,num,cost,arz,clr,chop,cmpnm,gh,date,pst,zn,pack,ext,bxknd,tel,percost,move,design) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at so51893932.so51893932.ContentHelper.save(ContentHelper.java:44)
        at so51893932.so51893932.box_2_dtent$1.onClick(box_2_dtent.java:78)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

修复以上问题,然后得到:-

08-18 23:09:46.051 2003-2003/so51893932.so51893932 E/SQLiteLog: (1) table content has no column named tel
08-18 23:09:46.051 2003-2003/so51893932.so51893932 E/SQLiteDatabase: Error inserting cel=d ertefa=t finalcost=z tol=s mn=n gl=k kl=l prjnm=v num=p cost=g arz=c clr=f chop=e cmpnm=a gh=j date=u pst=w zn=q pack=x ext=i prf=r bxknd=m tel=b percost=y move=o design=h
    android.database.sqlite.SQLiteException: table content has no column named tel (code 1): , while compiling: INSERT INTO content(cel,ertefa,finalcost,tol,mn,gl,kl,prjnm,num,cost,arz,clr,chop,cmpnm,gh,date,pst,zn,pack,ext,prf,bxknd,tel,percost,move,design) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at so51893932.so51893932.ContentHelper.save(ContentHelper.java:44)
        at so51893932.so51893932.box_2_dtent$1.onClick(box_2_dtent.java:78)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

以下版本的ContentHelper.java应该可以修复这些错误(请参见注释):-

public class ContentHelper {

    dbhelper dbhelper;
    public ContentHelper(Context context)
    {
        dbhelper = new dbhelper(context);
    }
    public Long save(content content)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put("cmpnm",content.cmpnm);
        contentValues.put("bxknd",content.bxknd);
        contentValues.put("tol",content.tol);
        contentValues.put("arz",content.arz);
        contentValues.put("ertefa",content.ertefa);
        contentValues.put("cost",content.cost);
        contentValues.put("num",content.num);
        contentValues.put("date",content.date);
        //contentValues.put("tel",content.tel); <<<< ERROR
        contentValues.put("tell",content.tel); //<<<< Corrected i.e. column name in table is tell not tel
        contentValues.put("design",content.design);
        contentValues.put("zn",content.zn);
        contentValues.put("pst",content.pst);
        contentValues.put("chop",content.chop);
        contentValues.put("gl",content.gl);
        contentValues.put("mn",content.mn);
        contentValues.put("gh",content.gh);
        contentValues.put("kl",content.kl);
        contentValues.put("pack",content.pack);
        contentValues.put("move",content.move);
        contentValues.put("ext",content.ext);
        contentValues.put("clr",content.clr);
        contentValues.put("cel",content.cel);
        //contentValues.put("profit",content.profit); //<<<< ERROR
        contentValues.put("prf",content.profit); //<<<< Corrected i.e. column name is prf not profit
        contentValues.put("finalcost",content.finalcost);
        contentValues.put("percost",content.percost);
        contentValues.put("prjnm",content.prjnm);

        dbhelper.opendb();
        Long a = dbhelper.db.insert(dbhelper.content,null,contentValues);
        dbhelper.closedb();
        return a;
    }
}

其他

最好仅依赖表和列名称的单一定义(例如,为每个名称适当定义的常量),而不是硬编码这样的名称(贯穿整个代码)(通过在dbhelper类中定义表名,然后根据Long a = dbhelper.db.insert(dbhelper.content,null,contentValues);使用dbhelper.content对其进行引用。