android - SQLite代码不起作用

android - SQLite代码不起作用,第1张

我正在尝试在我的Android应用程序中使用SQLite实现数据库 用户应输入他/她的第一个和第二个名字 当我运行该应用程序时,它不起作用,并显示此消息:“不幸的是,name.db已停止。”

我的代码如下

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

public class SDatabaseHelper extends DatabaseHelper {


private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="perInfo.db";
private static final String TABLE_NAME="Personal Informaition";
private static final String COLUMN_FNAME="First Name";
private static final String COLUMN_SNAME="Second Name";

SQLiteDatabase db;

public SDatabaseHelper( Context context)
{

    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE "   TABLE_NAME    "("
              COLUMN_FNAME   " TEXT, "
              COLUMN_SNAME   " TEXT )";

    db.execSQL(query);

}
public boolean insertdata(String fn, String sn ){
    db=this.getWritableDatabase();
    ContentValues values =new ContentValues();
    values.put(COLUMN_FNAME,fn);
    values.put(COLUMN_SNAME, sn);

    long result = db.insert(TABLE_NAME, null, values);
    if(result == -1)
        return false;
    else
        return true;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query="DROP TABLE IF EXISTS " TABLE_NAME;
    db.execSQL(query);
    onCreate(db);
}
}
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;


 public class persInfo extends ActionBarActivity {

EditText ftext, stext;
SDatabaseHelper dbHandler;
Button addbutt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pers_info);
    dbHandler=new SDatabaseHelper(this);

    ftext=(EditText) findViewById(R.id.name1);
    stext=(EditText) findViewById(R.id.name2);
    addbutt=(Button) findViewById(R.id.add);

    AddData();
}

public void AddData()
{
  addbutt.setOnClickListener(
          new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  boolean inserted = dbHandler.insertdata(ftext.getText().toString(),
                          stext.getText().toString());
                  if(inserted = true)
                      Toast.makeText(persInfo.this,"Data inserted",Toast.LENGTH_LONG).show();
                  else
                      Toast.makeText(persInfo.this,"Data dosnt inserted",Toast.LENGTH_LONG).show();

              }
     } ); } }
  public class personalInfo {

  String fname,sname;

   public void setFname(String fname){
        this.fname=fname;
   }
public String getFname(){
    return this.fname;
}
public void setSname(String Sname){
    this.sname=sname;
}
public String getSname(){
    return this.sname;
}
}

最佳答案:

1 个答案:

答案 0 :(得分:1)

这里的问题是你使用空格而不转义它们

private static final String TABLE_NAME="Personal Informaition";
private static final String COLUMN_FNAME="First Name";
private static final String COLUMN_SNAME="Second Name";

将破坏SQL表创建命令语法 如果你真的想要来使用空格,你必须逃避它们,如下所示:

private static final String TABLE_NAME="[Personal Informaition]";
private static final String COLUMN_FNAME="[First Name]";
private static final String COLUMN_SNAME="[Second Name]";

但我建议使用下划线,如下所示:

private static final String TABLE_NAME="Personal_Informaition";
private static final String COLUMN_FNAME="First_Name";
private static final String COLUMN_SNAME="Second_Name";
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复