2017-07-17 51 views
0

我有這樣的代碼 這是我在sqlite的第一個代碼,我把它從本教程http://www.youtube.com/watch?v=p8TaTgr4uKM我做什麼他做的視頻,但它不工作 DBclass不能添加數據的SQLite的Android

package com.catarmy.leader.ahmed.dbtest; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
public class Databasehelper extends SQLiteOpenHelper { 
public static final String DATABASE_NAME = "student.db"; 
public static final String TABLE_NAME = "student_table"; 
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 , MARK 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 mark) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_2, name); 
    contentValues.put(COL_3, surname); 
    contentValues.put(COL_4, mark); 
    long result = db.insert(TABLE_NAME, null, contentValues); 
    if (result == -1) 
     return false; 
    else 
     return true; 
} 
} 

主要活動類

package com.catarmy.leader.ahmed.dbtest;

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 studentDB; 
EditText name; 
EditText surname; 
EditText marks; 
Button add; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    name=(EditText)findViewById(R.id.Name); 
    surname=(EditText)findViewById(R.id.Surename); 
    marks=(EditText)findViewById(R.id.Marks) ; 
    add=(Button)findViewById(R.id.Add); 

    studentDB= new Databasehelper(this); 
    add(); 
    } 
public void add(){ 
add.setOnClickListener(
     new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
    //the boolean here to see if the data insert success 
    boolean isInserted= 
     studentDB.insertData(name.getText().toString(),surname.getText().toSt 
ring(),marks.getText().toString()); 
        Toast.makeText(MainActivity.this,"data insert 
="+isInserted,Toast.LENGTH_LONG).show(); 
      } 
     } 
); 
} 
} 

我的問題是,當我點擊添加按鈕敬酒味精說:「數據插入 =假」和表沒有被創建。

+0

精神缺失的空間**後EXISTS **在'db.execSQL( 「DROP TABLE IF EXISTS」 + TABLE_NAME);' –

+1

創建和插入語句中的標記列名稱不同。它在create語句中是'MARK',但'COL_4'對於插入的值爲'MARKS' – hibob

+0

我試過並且沒有結果 –

回答

0

我找到了解決辦法 我剛剛從 public static final String DATABASE_NAME = "student.db" 更改的數據庫名稱 public static final String DATABASE_NAME = "student" 謝謝

相關問題