2015-12-14 87 views
-1

我想用SQLite創建數據庫,但出現此錯誤。我不知道原因。爲什麼Logcat顯示「沒有這樣的表:MYTABLE(代碼1)」

這是我的代碼:

MainActivity.java。

import android.app.Activity; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    private final String TAG = "de.lecture.hska"; 
    EditText et_Name, et_Age; 

    MySQLiteOpenHelper mySQLiteOpenHelper; 
    MySQLAdapter mySQLAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     et_Name = (EditText) findViewById(R.id.Name); 
     et_Age = (EditText) findViewById(R.id.Age); 

     mySQLAdapter = new MySQLAdapter(this); 
    } 

    public void addData(View view) { 
     //SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase(); 

     String tmp = et_Name.getText().toString(); 
     long ok = mySQLAdapter.addMyData(tmp); 

     Toast.makeText(this, String.valueOf(ok), Toast.LENGTH_LONG).show(); 
    } 


    public void viewAllData (View view){ 
     String tmp = mySQLAdapter.showAll(); 
     Toast.makeText(this, tmp, Toast.LENGTH_LONG).show(); 
    } 
} 

MySQLAdapter:

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by caju1013 on 09.12.2015. 
*/ 

public class MySQLAdapter { 
    MySQLiteOpenHelper mySQLiteOpenHelper; 

    MySQLAdapter(Context context) { 
     mySQLiteOpenHelper = new MySQLiteOpenHelper(context); 
    } 

    public long addMyData (String Name){ 
     ContentValues cv = new ContentValues(); 
     cv.put(MySQLiteOpenHelper.TABLECOLUMN_NAME, "Probandoooo"); 

     SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase(); 

     return db.insert(MySQLiteOpenHelper.TABLE_NAME, null, cv); 
    } 

    public String showAll(){ 
     SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase(); 
     String [] array = {"Name"}; 

     Cursor cursor = db.query(MySQLiteOpenHelper.TABLE_NAME, null, MySQLiteOpenHelper.TABLECOLUMN_NAME+" = Name", null, null, null, null); 

     db.close(); 

     int index = cursor.getColumnIndex(MySQLiteOpenHelper.TABLECOLUMN_NAME); 
     StringBuffer stringBuffer = new StringBuffer(); 

     while (cursor.moveToNext()) { 
      String tempo = cursor.getString(index); 
      stringBuffer.append(tempo+"\n"); 
     } 

     return stringBuffer.toString(); 
    } 

    public class MySQLiteOpenHelper extends SQLiteOpenHelper { 
     private final static String TAG = "de.lecture.hska"; 
     private final static String DATABASE_NAME = "mydatabase.db"; 
     private final static int DATABASE_VERSION = 1; 

     private final static String TABLE_NAME = "MYTABLE"; 
     private final static String TABLECOLUMN_UID = "_id"; 
     private final static String TABLECOLUMN_NAME = "Name"; 
     private final static String TABLECOLUMN_AGE = "Age"; 

     private final static String CREATETABLE = "CREATE TABLE " + TABLE_NAME + "(" + TABLECOLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + TABLECOLUMN_NAME + " VARCHAR(255), " + TABLECOLUMN_AGE + " VARCHAR(255));"; 

     private final static String DROPTABLE = "DROP TABLE IF EXIST " + TABLE_NAME; 
     private Context mycontext; 

     public MySQLiteOpenHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 

      this.mycontext = context; 
      Toast.makeText(mycontext, "constructor called",Toast.LENGTH_LONG).show(); 
      Log.i(TAG, "Constructor called"); 
     } 

     public void onCreate(SQLiteDatabase sqLiteDatabase) { 
      try { 
       sqLiteDatabase.execSQL(CREATETABLE); 
       Toast.makeText(mycontext, "onCreate",Toast.LENGTH_LONG).show(); 
       Log.i(TAG, "onCreate"); 
      } catch (SQLException e) { 
       Toast.makeText(mycontext, e.toString(),Toast.LENGTH_LONG).show(); 
       Log.i(TAG, Log.getStackTraceString(e)); 
      } 
     } 

     public void onUpgrade (SQLiteDatabase sqLiteDatabase, int i, int i1){ 
      try { 
       sqLiteDatabase.execSQL(DROPTABLE); 
       onCreate(sqLiteDatabase); 
       Toast.makeText(mycontext, "OnUpgrade",Toast.LENGTH_LONG).show(); 
       Log.i(TAG, "onUpgrade"); 
      } catch (SQLException e) { 
       Toast.makeText(mycontext, e.toString(),Toast.LENGTH_LONG).show(); 
       Log.i(TAG, Log.getStackTraceString(e)); 
      } 
     } 
    } 
} 

MySQLiteOpenHelper:

import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import android.widget.Toast; 

/** 
* Created by caju1013 on 09.12.2015. 
*/ 
class MySQLiteOpenHelper extends SQLiteOpenHelper { 
    private final static String TAG = "de.lecture.hska"; 
    private final static String DATABASE_NAME = "mydatabase.db"; 
    private final static int DATABASE_VERSION = 1; 

    private final static String TABLE_NAME = "MYTABLE"; 
    private final static String TABLECOLUMN_UID = "_id"; 
    private final static String TABLECOLUMN_NAME = "Name"; 
    private final static String TABLECOLUMN_AGE = "Age"; 

    private final static String CREATETABLE = "CREATE TABLE " + TABLE_NAME + "(" + TABLECOLUMN_UID + " INTEGER PRIMARY KEY, " + TABLECOLUMN_NAME + " VARCHAR(255), " + TABLECOLUMN_AGE + " VARCHAR(255));"; 
    private final static String DROPTABLE = "DROP TABLE IF EXIST " + TABLE_NAME; 
    private Context mycontext; 

    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.mycontext = context; 
     Toast.makeText(mycontext, "constructor called",Toast.LENGTH_LONG).show(); 
     Log.i(TAG, "Constructor called"); 
    } 

    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
     try { 
      sqLiteDatabase.execSQL(CREATETABLE); 
      Toast.makeText(mycontext, "onCreate",Toast.LENGTH_LONG).show(); 
      Log.i(TAG, "onCreate"); 
     } catch (SQLException e) { 
      Toast.makeText(mycontext, e.toString(),Toast.LENGTH_LONG).show(); 
      Log.i(TAG, Log.getStackTraceString(e)); 
     } 
    } 

    public void onUpgrade (SQLiteDatabase sqLiteDatabase, int i, int i1){ 
     try { 
      sqLiteDatabase.execSQL(DROPTABLE); 
      onCreate(sqLiteDatabase); 
      Toast.makeText(mycontext, "OnUpgrade",Toast.LENGTH_LONG).show(); 
      Log.i(TAG, "onUpgrade"); 
     } catch (SQLException e) { 
      Toast.makeText(mycontext, e.toString(),Toast.LENGTH_LONG).show(); 
      Log.i(TAG, Log.getStackTraceString(e)); 
     } 
    } 

} 

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Name" 
     android:id="@+id/Name" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Age" 
     android:id="@+id/Age" 
     android:layout_below="@+id/Name" 
     android:layout_alignStart="@+id/Name" 
     android:layout_marginTop="64dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add Data" 
     android:id="@+id/button" 
     android:layout_below="@+id/Name" 
     android:layout_centerHorizontal="true" 
     android:onClick="addData"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="View all data" 
     android:id="@+id/button2" 
     android:layout_below="@+id/Age" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="37dp" 
     android:onClick="viewAllData"/> 
</RelativeLayout> 

回答

-1

你在這一行::

01問題
private final static String CREATETABLE = "CREATE TABLE " + TABLE_NAME + "(" + TABLECOLUMN_UID + " INTEGER PRIMARY KEY, " + TABLECOLUMN_NAME + " VARCHAR(255), " + TABLECOLUMN_AGE + " VARCHAR(255));"; 

由於SQLITE不支持varchar將其更改爲TEXT而不需要定義長度。

它應該是這樣的:

private final static String CREATETABLE = "CREATE TABLE " + TABLE_NAME + "(" + TABLECOLUMN_UID + " INTEGER PRIMARY KEY, " + TABLECOLUMN_NAME + " TEXT, " + TABLECOLUMN_AGE + " TEXT);"; 
相關問題