mardi 3 mars 2015

Search an SQL database

I have created an SQL database with a list of films. On my MainActivity, I have a EditText and a button, where I want the user to enter a film and press the button which will bring back the search results. How would I go about doing this?



public class DbHelper extends SQLiteOpenHelper {

private static final String ID = "id";
private static final String FILM = "film";
private static final String ACTOR = "actor";
private static final String ACTOR2 = "actor2";
private static final String DIRECTOR = "director";
private static final String DESCRIPTION = "description";
private static final String TABLE_NAME = "FILMTABLE";
private static final String DATABASE_NAME = "filmdatabase";
private static final int DATABASE_VERSION = 1;

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

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + TABLE_NAME + " (" +
ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
FILM + "TEXT NOT NULL, " +
ACTOR + "TEXT NOT NULL, " +
ACTOR2 + "TEXT NOT NULL, " +
DIRECTOR + "TEXT NOT NULL, " +
DESCRIPTION + "TEXT NOT NULL);"
);

Cursor countRows = db.rawQuery("SELECT count(*) FROM FILMTABLE", null);
countRows.moveToFirst();
int NumRows = countRows.getInt(0);
countRows.close();

if (NumRows == 0) {
ContentValues values = new ContentValues();

values.put("film", "Wolf of Wall Street");
values.put("actor", "Leonardo Dicaprio");
values.put("actor2", "Jonah Hill");
values.put("director", "Martin Scorses");
values.put("description", "True story of New York stockbroker Jordan Belfort. From the American dream to corporate greed, Belfort goes from penny stocks and righteousness to IPOs and a life of corruption in the late 80's.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Captain Philips");
values.put("actor", "Tom Hanks");
values.put("actor2", "Catherine Keener");
values.put("director", "Paul Greengrass");
values.put("description", "True story, Captain Phillips is a multi-layered examination of the 2009 hijacking of the U.S. container ship Maersk Alabama by a crew of Samali pirates.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "12 years a slave");
values.put("actor", "Chiwetel Ejiofer");
values.put("actor2", "Michael Williams");
values.put("director", "Steve McQueen");
values.put("description", "The film follows the experience of Solom Northup, an African American living with a wife and two children in New York, who is kidnapped and sold into slavery by men claiming to offer him work as a musician.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Dallas Buyers Club");
values.put("actor", "Matthew McConaughey");
values.put("actor2", "Jennifer Garner");
values.put("director", "Jean-Marc Vallee");
values.put("description", "Dallas buyers' club tells the story of Texan electrician Ron Woodroof and his battle with the medical establishment and pharmaceutical companies after being diagnosed HIV-positive in 1986.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "American Hustle");
values.put("actor", "Christian Bale");
values.put("actor2", "Amy Adams");
values.put("director", "David Russell");
values.put("description", "The story follows FBI agent Richie Dimaso as he blackmails conman Irving Rosenfield and his lover Sydney Prosser into helping him with a case after a successful investigation into the pairs foreign banking scam.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Last Vegas");
values.put("actor", "Robert De Niro");
values.put("actor2", "Morgan Freeman");
values.put("director", "Jon Turteltaub");
values.put("description", "When Billy, the groups sworn bachelor, finally proposes to his thirty-something girlfriend, the four head to Las Vegas with a plan to stop acting their age and relive their glory days.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Delivery Man");
values.put("actor", "Vince Vaughn");
values.put("actor2", "Cobie Smulders");
values.put("director", "Ken Scott");
values.put("description", "David Wozniak is in his fourties but still an eternal teenager who owes a lot of money and isn't successful, who finds out he has 142 of 553 children want to meet him.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "We're the Millers");
values.put("actor", "Jennifer Anniston");
values.put("actor2", "Jason Sudeikis");
values.put("director", "Rawson Marshall Thurber");
values.put("description", "Drug dealer David travels to Mexico to transport a shipment of Cannabis across the American border. In order to avoid suspicion he disguises himself as a family man.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "The Other Woman");
values.put("actor", "Cameron Diaz");
values.put("actor2", "Kate Upton");
values.put("director", "Nick Cassavettes");
values.put("description", "After discovering her boyfriend is married, a woman tries to get her ruined life back on track.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "22 Jump Street");
values.put("actor", "Channing Tatum");
values.put("actor2", "Jonah Hill");
values.put("director", "Chris Miller");
values.put("description", "After making their way through high school, big changes are in store for offers Schmidt and Jenko when they go deep undercover at a local college.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Guardians of the Galaxy");
values.put("actor", "Chris Pratt");
values.put("actor2", "Vin Diesel");
values.put("director", "James Gunn");
values.put("description", "An action-packed, epic space adventure expands the Marvel Cinematic Universe into the cosmo, where brash adventurer Peter Quill finds himself the object of an unrelenting hunt after a powerful villain has ambitions that threaten the universe.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "300");
values.put("actor", "Gerard Butler");
values.put("actor2", "Lena Headley");
values.put("director", "Zack Snyder");
values.put("description", "A 300 strong army of Spartans against Xerxes army of millions.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "I am legend");
values.put("actor", "Will Smith");
values.put("actor2", "Dash Minok");
values.put("director", "Francis Lawrence");
values.put("description", "When a man-made virus can't be contained, Neville finds himself the last human survivor in New York and potentially the world.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Signs");
values.put("actor", "Mel Gibson");
values.put("actor2", "Joaquin Pheonix");
values.put("director", "M Night Shyamalan");
values.put("description", "When crop circles start appearing in Pennsylvania, Widow Graham reassures his two children everything is going to be OK.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "The Notebook");
values.put("actor", "Rachel McAdams");
values.put("actor2", "Ryan Gosling");
values.put("director", "Nick Cassavettes");
values.put("description", "A passionate love story unfolds of a young couple whose fledging love affair was abruptly ended by circumstances and the outbreak of World War 2.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Jurassic Park");
values.put("actor", "Richard Attenborough");
values.put("actor2", "Sam Neill");
values.put("director", "Steven Speilberg");
values.put("description", "A scientist plans to open a theme park populates by live dinosaurs.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Jaws");
values.put("actor", "Richard Attenborough");
values.put("actor2", "Robert Shaw");
values.put("director", "Steven Speilberg");
values.put("description", "A large man-eating shark appears off the coast of Long Island whilst the sheriff is caught between a panicked community and a council who want to play down the deaths.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Dark Knight Rises");
values.put("actor", "Christian Bale");
values.put("actor2", "Tom Hardy");
values.put("director", "Christopher Nolan");
values.put("description", "A dangerous Bane, a masked terrorist whose ruthless plans for Gotham drive Bruce out of his self-imposed exile. ");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Gone Girl");
values.put("actor", "Ben Affleck");
values.put("actor2", "Rosamund Pike");
values.put("director", "David Fincher");
values.put("description", "Nick Dunne reports his wife missing but soon his lies, deceits and strange behaviour have everyone asking the dark question: Did Nick kill his wife?");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "The Hobbit");
values.put("actor", "Martin Freeman");
values.put("actor2", "Elijah Wood");
values.put("director", "Peter Jackson");
values.put("description", "Story follows Bilbo Baggins who suddenfuly finds himself in the company of 13 dwarves lost kingdom of the lonely mountain.");
db.insert("FILMTABLE", null, values);
values.clear();
}
}

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


}


Aucun commentaire:

Enregistrer un commentaire