2014-02-06 154 views
1

我开发了一个Android数据库应用程序。
问题是,当我点击添加按钮,然后一条消息显示数据已成功添加。
但是数据没有插入到数据库中。
然后我打开与观众的SQLite数据库,在那里我找不到任何我插入的数据...数据没有插入到Android上的SQLite数据库?

注册页面代码:

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(); 
      } 
     } 
    }); 
} 

数据库连接代码:

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); 
} 
} 

现在我该如何解决这个问题?谢谢

+0

LogCat中是否有任何错误?尝试从您的应用中读取相同的数据。 – Sanjeev

+0

发布你的logcat如果有错误? – Lokesh

+0

不,没有LogCat错误。我也试图从我的应用程序中读取数据,但失败... :( – user3279100

回答

1

在您的SQL错误。您没有在列名和类型之间提供空格。正确的应该是

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

不工作... :( – user3279100

+0

你有没有收到任何错误?你看过创建的表吗?删除你的数据库并尝试 –

+0

不,没有错误。我已经看到了使用sqlite查看器创建的表...但没有数据 – user3279100

相关问题