不幸的是,<appname>已停止。为什么会这样?完整的代码如下

时间:2017-04-14 14:52:58

标签: android sqlite

我在android studio中编写了这个完整的代码。我使用SQLite作为后端。但是一旦我点击“添加数据”按钮,我的应用就会停止。请帮忙。

// DatabaseHelper.java(文件名)  代码

package com.example.android.sqlite;

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

/**
 * Created by lenovo on 14-04-2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "User.db";
    public static final String TABLE_NAME = "User_info.db";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "SURNAME";
    public static final String COL_4 = "MARKS";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table" + TABLE_NAME + ("ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT, MARKS INTEGER"));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertdata(String name, String surname, String marks) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(COL_2, name);
        cv.put(COL_3, surname);
        cv.put(COL_4, marks);
        long result = db.insert(TABLE_NAME, null, cv);
        if (result == -1)
            return false;
        else
            return true;


    }
}

// MainActvity.java(文件名) //代码

package com.example.android.sqlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    DatabaseHelper my_db;
    EditText editN,editS,editM;
    Button butt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        my_db = new DatabaseHelper(this);
        editN= (EditText)findViewById(R.id.text1);
        editS=(EditText)findViewById(R.id.text2);
        editM=(EditText)findViewById(R.id.editText);
        butt=(Button)findViewById(R.id.button);
        adddata();

    }
    public void adddata(){
        butt.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    boolean isinserted = my_db.insertdata(editN.getText().toString(),editS.getText().toString(),editM.getText().toString());
                    if(isinserted==true)
                        Toast.makeText(MainActivity.this,"Data inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this,"Data insertion failed",Toast.LENGTH_LONG).show();

                }
            }
        );
    }

}

1 个答案:

答案 0 :(得分:0)

您的TABLE_NAME = "User_info.db"包含.。似乎不允许这样做:https://stackoverflow.com/a/3694305/4322687

另外,在以下行中: db.execSQL("create table" + TABLE_NAME + ("ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT, MARKS INTEGER")); 第二对括号看起来像是里面引号。